Search code examples
c#outlookvsto

In an Outlook 2013 C# VSTO project, why does the Explorer SelectionChange event fire twice


In my Outlook 2013 C# VSTO project I've noticed that the Explorer SelectionChange event is firing twice. I thought it must be due to a bug in my code (like hooking the event handler up twice), but I couldn't find any such bugs.

So I went back to basics and created a little VSTO Outlook 2013 Addin test project, and the same thing happens there too. The Explorer SelectionChange event is fired twice.

public partial class ThisAddIn
{
    private Explorer _activeExplorer;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        _activeExplorer = Application.Explorers[1];

        _activeExplorer.SelectionChange += _activeExplorer_SelectionChange;
    }

    private void _activeExplorer_SelectionChange()
    {
        System.Diagnostics.Debug.WriteLine("_activeExplorer_SelectionChange : " + DateTime.Now.ToString());
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #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
}

I can code around this, but surely the SelectionChange event shouldn't be firing twice.

Any ideas why the SelectionChange event is firing twice? And what can I do to make it fire only once (other than writing my own code to check if the selection has changed)?


Solution

  • You need to turn off the Reading Pane in Outlook:

    enter image description here

    After you turn it off you will get only event at a time.