Office Event Handling in C#

D

Darren

Hi,

Can anyone please direct me to some example C# code that shows how to
trap a document open event (ideally for MS Project 2000 but Word or
Excel would probably do).

MS Project is really weird - using Visual Studio .NET Intellisense I
can easily browse the MS Project object model and quickly implement
events such as NewProject or ProjectBeforeClose but I cannot find a
way of trapping the open project event (my addin needs to know the
names of all loaded projects)

The code below registers a "ProjectBeforeClose" event handler

projApp = (MSProject.Application)application;

projApp.ProjectBeforeClose += new
MSProject._EProjectApp_ProjectBeforeCloseEventHandler(projApp_ProjectBeforeClose);

This works fine but there seems to be no equivalent way to trap an
open event.
I know the open event exists (I can see it
"_EProjectDoc_OpenEventHandler") but there seems to be no way to
easily register a handler.

Please help me if you can.

Thanks in advance,
Darren.
 
M

Michael R

This seems to work for any event. You can change the oType and EventHandler
to use different Word versions.
//add event handlers via reflection
BindingFlags oBindFlags = BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic;
Type oType;
EventInfo oEvent;
MethodInfo oMethod;
object result;

// 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) }
);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top