When you create the Word object you can create an Event handler for the Word
DocumentClose event.
This handler is in your .Net application. It is then trivial to call any
object within your code.
Below is a way to create the event handler for the DocumentOpen event. You
can change the Word.ApplicationEvents3_DocumentOpenEventHandler to handle
any Word XP event. The only caveat is the event handler must have the
correct signature.
//get the version of Word we are running
WordVersion = (string)appObj.GetType().InvokeMember("Version",
BindingFlags.GetProperty , null, appObj , null);
//add event handlers via reflection
BindingFlags oBindFlags = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic;
Type oType;
EventInfo oEvent;
MethodInfo oMethod;
object result;
// add our handlers to the events based on app
if ( WordVersion == OfficeXP ) // OfficeXP is a constant defined as 10.0
{
// the Word XP version 10.0 events
oType = typeof(Word.ApplicationEvents3_Event);
//DocumentOpen
oEvent = oType.GetEvent("DocumentOpen", oBindFlags);
oMethod = oEvent.GetAddMethod();
result = oMethod.Invoke(appObj, new object [] { new
Word.ApplicationEvents3_DocumentOpenEventHandler(this.DocOpenEventHandler) }
);
}
else if ( WordVersion == Office2K )
{
// The Word 2000 version 9.0 events
oType = typeof(Word.ApplicationEvents2_Event);
//DocumentOpen
oEvent = oType.GetEvent("DocumentOpen", oBindFlags);
oMethod = oEvent.GetAddMethod();
result = oMethod.Invoke(appObj, new object [] { new
Word.ApplicationEvents2_DocumentOpenEventHandler(this.DocOpenEventHandler) }
);
}
//programmatically open a Word document
if ( WordVersion == OfficeXP )
appObj.Documents.Open(ref sFile, ref No, ref No, ref No, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
else if ( WordVersion == Office2K )
appObj.Documents.Open2000(ref sFile, ref No, ref No, ref No, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref
oMissing, ref oMissing);
else if ( WordVersion == ? )
appObj.Documents.OpenOld(...); // I assume this will open a Word97
document??
else
throw new Exception("Unknown Word Version");
//event handler - called when a document is opened
private void DocOpenEventHandler(Word.Document oDoc)
{
// call your code
}

Signature
Michael R
NOSPAMcre1@tampabay.rr.com
> Hi,
>
[quoted text clipped - 7 lines]
>
> Adam
AP - 30 Oct 2003 17:08 GMT
Thanks for your response. I already tried this approach, and the event does
not get fired all the time, which is why I am trying to do things this way.
We are using word 2000.
Adam
> When you create the Word object you can create an Event handler for the Word
> DocumentClose event.
[quoted text clipped - 27 lines]
> oMethod = oEvent.GetAddMethod();
> result = oMethod.Invoke(appObj, new object [] { new
Word.ApplicationEvents3_DocumentOpenEventHandler(this.DocOpenEventHandler) }
> );
> }
[quoted text clipped - 7 lines]
> oMethod = oEvent.GetAddMethod();
> result = oMethod.Invoke(appObj, new object [] { new
Word.ApplicationEvents2_DocumentOpenEventHandler(this.DocOpenEventHandler) }
> );
> }
[quoted text clipped - 33 lines]
> >
> > Adam