Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2003

Tip: Looking for answers? Try searching our database.

Accessing .NET class instance from Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AP - 29 Oct 2003 18:20 GMT
Hi,

My .NET application has a dll with a singleton class in it. In this
singleton class I create and launch a word document. In this word document I
would like to add a close event handler that will call back into the .NET
singleton class. Is this possible? How would I get a handle on this
singleton class in VBA?

Thanks,

Adam
Michael R - 30 Oct 2003 10:09 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.