Luc VdV
Luc VdV

Reputation: 1198

In an Outlook VSTO add-in, C# won't let you hook an event handler to Application.Quit

Microsoft documentation says (https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/ee720183(v=office.14)#practice-2-detecting-when-outlook-is-shutting-down)

To detect that Outlook is shutting down, you can use the Quit event of the Application object in the Outlook object model to receive a notification that the process is shutting down. Be sure that you respond quickly to the event and return control to Outlook as quickly as possible.

But it seems that can only be done in VB.Net, C# won't let me. There is a method called Quit() as well as an event called Quit, and when I try to hook up an event handler to that event, the C# compiler says you can't use += on a method group.

BTW, I tried: you can't create anything with an event and a method having the same name in C# either. The language doesn't allow it.

The example below is the VSTO project template code for an Outlook plug-in, with just a method QuitHandler added, and an attempt to hook it to the Quit event.

When you are entering the code, intellisense will show both the Quit event and the Quit method to choose from, but picking the event results in code that doesn't compile.

Is there a way to explicitly tell the compiler it's the quit event you intend to use?

namespace OutlookAddIn2
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.Quit += MyQuitHandler;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
        }

        void MyQuitHandler()
        {

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

Compiler output (you may ignore the en-US warning, I've had that in all versions of Visual Studio as far as my memory goes back, AFAICT it is caused by installing Office and Visual Studio on the same machine.)

1>------ Build started: Project: OutlookAddIn2, Configuration: Debug Any CPU ------
1>CSC : warning CS2038: The language name 'en-US' is invalid.
1>D:\Temp\OutlookAddIn2\ThisAddIn.cs(7,10,7,26): error CS1656: Cannot assign to 'Quit' because it is a 'method group'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The problem doesn't exist when I try the same in VB.Net, this compiles OK:

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        AddHandler Application.Quit, AddressOf MyQuitHandler
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub MyQuitHandler()

    End Sub

End Class

This is using Vusual Studio 2017 with Resharper 2017.3.1.

I don't think Resharper is to blame in this particular case: I've seen similar error messages when its symbol cache goes bad, but that never results in code that doesn't compile. On the contrary, it results in code that compiles and works despite the errors the code editor indicates.

[Edit] BTW, it doesn't work like this either, I would assume it is implicit anyway

Application.Quit += new ApplicationEvents_11_QuitEventHandler(MyQuitHandler);

Upvotes: 2

Views: 806

Answers (1)

Mark Z.
Mark Z.

Reputation: 2447

Duplicate of No Application Quit event in Outlook?

Answer (which I tested and confirmed to work):

((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!!");
}

Upvotes: 2

Related Questions