7

I'm using the 12.0 Interop library, which is the default for Outlook 2007. I'm actually aiming for Outlook 2003 to 2010 integration with a code example that registers to a quit event.

Even though the docs say that there is an application Quit event for the Outlook app, I can't find it in the Outlook.Application object implementation.

Visual Studio 2010 seems to identify Quit as a method:

Quit seems to be a method, not an event

Question:

How would one register to the Outlook application's Quit event? (if there is one, or any event that is triggered when the application quits) If possible provide some example code.

Thanks!

Alex
  • 7,432
  • 20
  • 75
  • 118
  • I see that its Office 2007 API, is your API of that version.? – Tigran Jul 13 '11 at 10:24
  • I'm targeting Outlook 2003 through 2010. The Interop dll file is version 12.0, which I think is from Office 2007. – Alex Jul 13 '11 at 11:07
  • What are you trying to do here? I'm asking because the Application.Quit Event isn't really helpful, since by the time it fires all objects have been released. – JimmyPena Nov 16 '11 at 18:44
  • @JP., I guess I was trying to figure out when Outlook is quitting so that I may react to this in my application, which is connected to the Outlook MAPI store. I eventually ended up using the `Process.Exited` event as mentioned by Tigran. – Alex Nov 18 '11 at 10:38
  • 3
    The problem is a name conflict between the `Quit` method and the `Quit` event, both of which are defined on separate interfaces. The method shadows the event, and you have to explicitly cast the Application object to the `Microsoft.Office.Interop.Outlook.ApplicationEvents_11_Events` interface to access the event. – Andreas Baus Jan 23 '12 at 12:04

2 Answers2

28
((Outlook.ApplicationEvents_11_Event)Application).Quit 
+= new Outlook.ApplicationEvents_11_QuitEventHandler(ThisAddIn_Quit);

void ThisAddIn_Quit()
{
   System.Windows.Forms.MessageBox.Show("bye bye problem, I found the solution!!");
}
VMh
  • 1,300
  • 1
  • 13
  • 19
  • 2
    Great, indeed. Needed this because according to MSDN, ThisAddin_Shutdown is no longer called for Outlook 2007SP2, Outlook 2010, and up. [link](http://msdn.microsoft.com/en-us/library/ee720183%28v=office.14%29.aspx) – public wireless Sep 05 '13 at 21:49
  • 1
    This should be the excepted answer. – user2102508 Mar 22 '19 at 10:50
2

Just try to give a solution: may be you can get Outlook process and listen for Process.Exited event.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Thanks for the answer. I assume you mean the `Exited` event within a `Process`, right? – Alex Jul 13 '11 at 10:27
  • Yes, that is one way of doing it, but because I am also aiming for Outlook 2003 and 2007 SP1, the OUTLOOK process doesn't close if there is an application still using it (something to do with COM). So I'm still interested in knowing if there is an event triggered when the user presses the **X** button. – Alex Jul 13 '11 at 13:08