none
ExplorerEvents_10_SelectionChangeEventHandler does not fire under Windows 8 RRS feed

  • Question

  • Hello,

    we developed an addon for Outlook 2013/2010/2007 with VS2013. The addon is supposed to react on the SelectionChanged event of the ExplorerEvents_10_Event interface. In most cases we run the Outlook versions under Windows 7, and for that the addon works perfectly as it should, however, in one case there is an Outlook 2007 running under Windows 8 and here the event is not fireing. The code:

            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
    Globals.ThisAddIn.Application.ActiveExplorer().SelectionChange += OnActiveExplorerSelectionChange;
            }
    
    		private void OnActiveExplorerSelectionChange()
    		{
    MessageBox.Show("Tadaa");
    		}
    

    With Windows 8 and Outlook 2007 the message box does not show up. We have not tried other Outlook versions under Windows 8 yet, due to some circumstances we would rather use that as the last option. So any help or clue is very much apprechiated. Thank you!

    Marc

    Tuesday, July 1, 2014 8:38 AM

Answers

  • Hello Marc,

    An instance of the Explorer class is swiped by the garbage collector because the scope is limited to the Startup event handler. You need to declare the explorer instance at the class level if you want to continue receiving events. For example:

    private Explorer explorer = null;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        explorer = 
    Globals.ThisAddIn.Application.ActiveExplorer();
        explorer.SelectionChange += OnActiveExplorerSelectionChange;
    }
    

    Also I have noticed that you don't release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    • Marked as answer by Crowley1970 Tuesday, July 1, 2014 10:31 AM
    Tuesday, July 1, 2014 9:09 AM

All replies

  • Hello Marc,

    An instance of the Explorer class is swiped by the garbage collector because the scope is limited to the Startup event handler. You need to declare the explorer instance at the class level if you want to continue receiving events. For example:

    private Explorer explorer = null;
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        explorer = 
    Globals.ThisAddIn.Application.ActiveExplorer();
        explorer.SelectionChange += OnActiveExplorerSelectionChange;
    }
    

    Also I have noticed that you don't release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about this in the Systematically Releasing Objects article in MSDN.

    • Marked as answer by Crowley1970 Tuesday, July 1, 2014 10:31 AM
    Tuesday, July 1, 2014 9:09 AM
  • Thank you, Eugene. That was it. :-)
    Tuesday, July 1, 2014 10:31 AM