1

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?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
tobre
  • 1,347
  • 3
  • 21
  • 53
  • Have you tryed `word.Quit += Quit` as [this](https://stackoverflow.com/questions/29840120/cannot-assign-to-method-because-it-is-a-method-group) answer suggest? – ikerbera Nov 21 '18 at 13:25
  • 1
    There is no `Quit` event handler. If you look on [Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.applicationclass.quit?view=word-pia#Microsoft_Office_Interop_Word_ApplicationClass_Quit_System_Object__System_Object__System_Object__) you'll see that `Quit` is a void method. You can see other entries at [this link](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.applicationclass?view=word-pia) which show the various ApplicationEventsX_Event_Quit events – A Friend Nov 21 '18 at 13:31
  • That does not work either. `Cannot choose method from method group. Did you intend to invoke the method?` – tobre Nov 21 '18 at 13:31
  • @A Friend: Intellisense shows both a method and an EventHandler with the name Quit.This is why I asked the question. But I see that the documentation does not confirm the existence of this EventHandler. – tobre Nov 21 '18 at 13:36
  • @A Friend: I am using the [Application](https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.applicationevents4_event.quit?view=word-pia) class and not the ApplicationClass. The Application class does have a Quit event. – tobre Nov 21 '18 at 13:44
  • `Application` is an interface, which is implemented into `ApplicationClass` – A Friend Nov 21 '18 at 13:49
  • You must need to get the cast right, it is ((ApplicationEvents4_Event)word).Quit += yadayada; – Hans Passant Nov 21 '18 at 14:09
  • @Hans Passant: You nailed it. I just found that [here](https://social.msdn.microsoft.com/Forums/vstudio/en-US/593a5978-c249-4ecb-9c00-855d550a8818/applicationquit-event-in-word?forum=vsto). Thank you. Do you want to post that as an answer and I will accept it as the correct one. – tobre Nov 21 '18 at 14:17
  • Just share what you discovered in your own post and mark it as the answer. – Hans Passant Nov 21 '18 at 14:18

1 Answers1

1

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;
tobre
  • 1,347
  • 3
  • 21
  • 53