Announcement

Collapse
No announcement yet.

Word Object Modell

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

  • Word Object Modell

    <p>Hallo Zusammen,</p>
    Ich möchte innerhalb einer Windows-Anwendung (C#) ein Word Dokument laden und mithilfe eines Events auf die Änderungen des Textes reagieren.<br>
    Das Laden des Dokuments funktioniert.<br>
    Kann mir jemand helfen, wie ich eine entsprechende Ereignis-Definition in Visual Studio .Net erstelle?<br>
    Vielen Dank.

  • #2
    Hallo,
    das folgende Beispiel für das Auswerten von Word-Events greift auf die PIA (Primary Interop Assembly) von Word 2003 zurück. Wenn zum Zeitpunkt der Office 2003-Installation das .NET Framework 1.1 noch nicht installiert war oder das Office-Setup nur im Standard-Modus ausgeführt wurde, sind die PIA's für Office 2003 nicht registriert. Zur Installation der PIA's muss das Office 2003-Setup über die <i>Systemsteuerung | Software</i> im Wartungs-Modus erneut ausgeführt werden: <i>Features hinzufügen oder entfernen</i> | Checkbox <i>Erweiterte Anpassung von Anwendungen</i> ankreuzen | Option <i>.NET-Programmierunterstützung</i> auswählen.
    <br>
    Im Projekt wird dann ein Verweis auf die <i>Microsoft Word 11.0 Object Library</i> (Registerseite COM) hinzugefügt. Wenn die PIA von Word 2003 korrekt im System registriert ist, legt VS.NET dabei keine eigene Interop Assembly an, sondern bindet nur einen Verweis auf die bereits im GAC registrierte PIA von Word 2003 ein.
    <br>
    Das Auswerten des Quit-Events von Word sieht dann zum Beispiel so aus (über <i>using Word2003 = Microsoft.Office.Interop.Word;</i> wird ein Alias für das Application-Objekt von Word aktiviert, damit es an dieser Stelle zu keinen Kollisionen mit dem Application-Objekt von .NET kommt):
    <code>
    <font color="#008000">// using System.Runtime.InteropServices;</font>
    <font color="#008000">// using Word2003 = Microsoft.Office.Interop.Word;</font>

    <font color="#0000FF">private</font> Word2003.<font color="#008080">Application</font> aWordApp;
    <font color="#0000FF">private</font> Word2003.ApplicationEvents2_Event aEventIntf;
    <font color="#0000FF">private</font> <font color="#0000FF">string</font> sDOT;
    <font color="#0000FF">private</font> <font color="#0000FF">string</font> sDOC;

    <font color="#0000FF">private</font> <font color="#0000FF">void</font> Form1_Load(<font color="#0000FF">object</font> sender, System.<font color="#008080">EventArgs</font> e)
    {
    sDOT = System.IO.<font color="#008080">Path</font>.Combine(<font color="#008080">Application</font>.StartupPath, "Word2003QuitEventDemo.dot");
    sDOC = System.IO.<font color="#008080">Path</font>.Combine(<font color="#008080">Application</font>.StartupPath, "Ergebnis.doc");
    }

    <font color="#0000FF">private</font> <font color="#0000FF">void</font> ButtonStart_Click(<font color="#0000FF">object</font> sender, System.<font color="#008080">EventArgs</font> e)
    {
    aWordApp = <font color="#0000FF">new</font> Word2003.ApplicationClass();
    aWordApp.Visible = <font color="#0000FF">true</font>;
    aEventIntf = aWordApp <font color="#0000FF">as</font> Word2003.ApplicationEvents2_Event;
    <font color="#008000">// Ereignisbehandlung für das Quit-Event aktivieren</font>
    aEventIntf.Quit +=<font color="#0000FF">new</font> Microsoft.Office.Interop.Word.ApplicationEvents2_Q uitEventHandler(aEventIntf_Quit);
    ButtonStart.Enabled = <font color="#0000FF">false</font>;
    ButtonInsert.Enabled = <font color="#0000FF">true</font>;
    ButtonQuit.Enabled = <font color="#0000FF">true</font>;
    ListBoxLog.Items.Add("Word2003.<font color="#008080">Application</font> erzeugt...");
    }

    <font color="#0000FF">private</font> <font color="#0000FF">void</font> aEventIntf_Quit()
    {
    ListBoxLog.Items.Add(" --&gt; Quit-Ereignis von Word2003 eintroffen");
    aWordApp = <font color="#0000FF">null</font>;
    ButtonStart.Enabled = <font color="#0000FF">true</font>;
    ButtonInsert.Enabled = <font color="#0000FF">false</font>;
    ButtonQuit.Enabled = <font color="#0000FF">false</font>;
    }

    <font color="#0000FF">private</font> <font color="#0000FF">void</font> ButtonInsert_Click(<font color="#0000FF">object</font> sender, System.<font color="#008080">EventArgs</font> e)
    {
    <font color="#008000">// Neues Dokument von der DOT-Vorlage ableiten</font>
    Word2003.Documents aWordDocCol;
    Word2003.Document aWordDoc;
    aWordDocCol = aWordApp.Documents;
    <font color="#0000FF">object</font> Template = sDOT;
    <font color="#0000FF">object</font> NIL = <font color="#008080"><font color="#008080">Type</font></font>.<font color="#008080">Missing</font>;
    aWordDoc = aWordDocCol.Add(<font color="#0000FF">ref</font> Template, <font color="#0000FF">ref</font> NIL, <font color="#0000FF">ref</font> NIL, <font color="#0000FF">ref</font> NIL);
    <font color="#008000">//</font>
    <font color="#008000">// irgend was mit diesem Dokument machen...</font>
    <font color="#008000">//</font>
    <font color="#008080">MessageBox</font>.Show("Der neue Text sollte nun im Dokument zu sehen sein!");
    <font color="#008000">// Dokument speichern</font>
    <font color="#0000FF">object</font> FileName = sDOC;
    aWordDoc.SaveAs(<font color="#0000FF">ref</font> FileName,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,
    <font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,
    <font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL,<font color="#0000FF">ref</font> NIL);
    aWordDoc.Close(<font color="#0000FF">ref</font> NIL, <font color="#0000FF">ref</font> NIL, <font color="#0000FF">ref</font> NIL);
    <font color="#008000">// Aufräumen</font>
    <font color="#008080">Marshal</font>.ReleaseComObject(aWordDoc);
    <font color="#008080">Marshal</font>.ReleaseComObject(aWordDocCol);
    }

    <font color="#0000FF">private</font> <font color="#0000FF">void</font> ButtonQuit_Click(<font color="#0000FF">object</font> sender, System.<font color="#008080">EventArgs</font> e)
    {
    <font color="#0000FF">if</font> (aWordApp != <font color="#0000FF">null</font>)
    {
    aEventIntf.Quit -= <font color="#0000FF">new</font> Microsoft.Office.Interop.Word.ApplicationEvents2_Q uitEventHandler(aEventIntf_Quit);
    <font color="#0000FF">object</font> NIL = <font color="#008080"><font color="#008080">Type</font></font>.<font color="#008080">Missing</font>;
    aWordApp.Quit(<font color="#0000FF">ref</font> NIL, <font color="#0000FF">ref</font> NIL, <font color="#0000FF">ref</font> NIL);
    <font color="#008080">Marshal</font>.ReleaseComObject(aWordApp);
    aEventIntf = <font color="#0000FF">null</font>;
    aWordApp = <font color="#0000FF">null</font>;
    ButtonStart.Enabled = <font color="#0000FF">true</font>;
    ButtonInsert.Enabled = <font color="#0000FF">false</font>;
    ButtonQuit.Enabled = <font color="#0000FF">false</font>;
    ListBoxLog.Items.Add("... Word2003.<font color="#008080">Application</font> zerstört.");
    }
    }
    </code>
    P.S: Bei der Automation ist der Aufwand mit C# spürbar höher als das bei VB.NET der Fall ist

    Comment

    Working...
    X