views:

50

answers:

1

From Silverlight 4, it's pretty easy to start Word and let the user do something:

dynamic word = System.Runtime.InteropServices.Automation.CreateObject("Word.Application");
word.Visible = true;
word.Documents.Open("test.doc");

MS Word exposes a Quit event[1]. I'd like to handle this event, but for the life of me I can't figure out how. I tried to do this:

public delegate void WordQuitEventHandler(object sender, ref bool cancel);
public event WordQuitEventHandler OnQuit;
private void WordOnQuit(dynamic sender, ref bool cancel)
{
    if (OnQuit != null)
    {
        OnQuit(this, ref cancel);
    }
}

and then do

word.Quit = WordOnQuit;

or

word.Quit += WordOnQuit;

But it's not possible to assign the delegate for WordOnQuit to the dynamic object word.Quit. So how to capture this event?

[1] http://msdn.microsoft.com/en-us/library/aa211898(v=office.11).aspx

A: 

I haven't tried this, but it might work:

word.Quit += new WordQuitEventHandler(WordOnQuit);

Basically the compiler doesn't know what type you want to convert the method group to at the moment - the code above should give it enough information.

Jon Skeet
I tried it, it says it doesn't know what to do with the delegate:"Unhandled object type [Test+WordQuitEventHandler] in ConvertObjectToInteropValue"
JoostM
I found an example, but I can't really try it until I'm at work again:http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.automation.automationevent(v=VS.95).aspx
JoostM