Announcement

Collapse
No announcement yet.

Microsoft.Office.Interop.XXXX Versionsunabängig integrieren

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Microsoft.Office.Interop.XXXX Versionsunabängig integrieren

    Hallo.

    Ich habe ein Problem beim Entwickeln mit den Interop DLLs von Microsoft Office.
    Gibt es irgendwie eine Möglichkeit die Funktionalität die sich hinter diesen DLLs verbirgt Versionsunabhängig zu machen? Wenn ich (benutze Office 2003) ein solches Projekt erstelle und mein Kollege (nutzt Office 2007) möchte es ebenfalls nnutzen, so scheittert dies, da die passende Version die DLL nicht auf seinem Rechner gefunden wird.

    Hat da jemand eine Idee wie man so etwas realisieren kann?

    Vielen Dank schon mal im Voraus für jegliche Hilfe.

    Gruß
    Manuel Büchler

  • #2
    AFAIK geht das nur mit VB.NET.

    Comment


    • #3
      http://entwickler-forum.de/showthread.php?t=2232
      Schöne Grüße, Mario

      Comment


      • #4
        Hallo Manuel!

        Wie du bereits gesehen hast ist das mit C# ein wenig komplexer - ABER moeglich und es funkt wirklich auf allen Office - Versionen (95 - 2007) :-)

        Ich weiss nicht ob du es noch brauchst, aber hier waere ein einfaches Beispiel, welches einen beliebigen Text in ein neues Word Dokument einfuegt:

        Code:
                public static void CreateWordDocumentLateBinding(string documentText, int linebreaksBeforeText) 
                {
                    documentText = string.IsNullOrEmpty(documentText) ? string.Empty : documentText.Trim();
        
                    // get the prog id and create an instance of it
                    object objWordApp;
                    objWordApp = OfficeFunctions.GetWordAsObject;   
                    if (objWordApp == null) { return; }
        
                    // create Documents, Add a new document and Activate it
                    object objWordDocs =
                        objWordApp.GetType().InvokeMember("Documents", System.Reflection.BindingFlags.GetProperty, null, objWordApp,
                        null, System.Globalization.CultureInfo.InvariantCulture);
                    object objWordDoc =
                        objWordDocs.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, objWordDocs,
                        null, System.Globalization.CultureInfo.InvariantCulture);
                    objWordDoc.GetType().InvokeMember("Activate", System.Reflection.BindingFlags.InvokeMethod, null, objWordDoc,
                        null, System.Globalization.CultureInfo.InvariantCulture);
        
                    // use the Selection to add some TypeParapgraphs (blank lines) and TypeText the text
                    object objWordSel =
                        objWordApp.GetType().InvokeMember("Selection", System.Reflection.BindingFlags.GetProperty, null, objWordApp,
                        null, System.Globalization.CultureInfo.InvariantCulture);
        
                    for (int linebreak = 0; linebreak < linebreaksBeforeText; linebreak++)
                    {
                        objWordSel.GetType().InvokeMember("TypeParagraph", System.Reflection.BindingFlags.InvokeMethod, null,
                            objWordSel, null, System.Globalization.CultureInfo.InvariantCulture);
                    }
        
                    objWordSel.GetType().InvokeMember("TypeText", System.Reflection.BindingFlags.InvokeMethod, null, objWordSel,
                        new object[] { documentText }, System.Globalization.CultureInfo.InvariantCulture);
        
                    // make word Visible and bring to front
                    objWordApp.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.SetProperty, null, objWordApp,
                        new object[] { true }, System.Globalization.CultureInfo.InvariantCulture);
                    objWordApp.GetType().InvokeMember("Activate", BindingFlags.InvokeMethod, null, objWordApp, null,
                         System.Globalization.CultureInfo.InvariantCulture);
        
                    objWordSel = null;
                    objWordDoc = null;
                    objWordDocs = null;
                    objWordApp = null;
                }
        
                /// <summary>
                /// Returns either the Word.Application object or null
                /// </summary>
                public static object GetWordAsObject
                {
                    get
                    {
                        object objWordApp;
                        if (OfficeFunctions._wordObject == null)
                        {
                            // find and create an instance of Word.Application
                            Type objClassType = Type.GetTypeFromProgID("Word.Application");
                            try
                            {
                                objWordApp = Activator.CreateInstance(objClassType);
                            }
                            catch (System.ArgumentException ex)
                            {
                                Helpers.ShowMessageBox(ex.Message);
                                objWordApp = null;
                            }
                            if (objWordApp == null) { Helpers.ShowMessageBox("Word konnte nicht als Objekt instanziert werden."); }
                            objClassType = null;
                            OfficeFunctions._wordObject = objWordApp; 
                        }
                        else { objWordApp = OfficeFunctions._wordObject; }
                        return objWordApp;
                    }
                }
        Hoffe das hilft dir weiter.
        BTW: Bei EXCEL und OUTLOOK funkt es aehnlich.
        *-- robert.oh. --*

        Comment

        Working...
        X