Search code examples
c#ms-wordoffice-interop

How to register for an EventHandler when there is a method with the same name


I have an instance of the Microsoft.Office.Interop.Word in a variable

Application word;

Now I want to register a method for the Quit Eventhandler.

word.Quit += onWordQuit;

The problem is, that there is also a method called Quit. The compiler complains that

"Cannot assign to 'Quit' because it is a method group.
Reference 'Quit' is a 'method group'. The assignment target must be an assignable variable, property or indexer

I found this blog post from 2004 about this subject. But when I cast Quit like so:

(ApplicationEvents4_QuitEventHandler)word.Quit += onWordQuit;

I get the error

No overload for Quit matches delegate ApplicationEvents4_QuitEventHandler.

How can I register to the Quit Event Handler in this case?


Solution

  • The way to register for the EventHandler in this case is (as Hans Passant and this post pointed out) is:

    ((ApplicationEvents4_Event)word).Quit += onWordQuit;