Search code examples
outlookvstointeropoutlook-addinoffice-addins

VSTO Outlook: Detect when Outlook is being closed


I have a custom task pane placed at the top. Since I cannot hide close button nor the context menu, I decided to show a message to the user when he/she tries to close it saying that the task pane cannot be closed.

I show this message when the task pane visibility changes to false (it is the only way I can detect if user is trying to close it), so within the VisibleChanged event handler I show a messagebox.

this.myCtp = Globals.ThisAddIn.CustomTaskPanes.Add(myControl, "myTitle", this.Window);
this.myCtp.VisibleChanged += myCtp_VisibleChanged;


private void myCtp_VisibleChanged(object sender, EventArgs e)
{
    // here is show my messagebox
{

The problem with this is that when Outlook is exiting, this messagebox appears to the user and I do not want this. So what can I do to prevent the messagebox to appear when Outlook is exiting?

I have thought of using a boolean variable that is set in the Application Quit event handler and then this variable is consulted in the myCtp_VisibleChanged event handler and according to this show or not the messagebox.


Solution

  • You can also trap the Explorer.Close event - if it is the last Explorer (Application.Explorers.Count = 1), you know Outlook is closing.