COMException when checking ActiveExplorer().Selection RRS feed

  • Question

  • I am fairly new to Outlook development but am encountering an issue with getting the currently selected items in the current Explorer. (This is all using C#, VS2012 and Office 2013).

    Outlook.Explorer currentExplorer = null;
    Outlook.Selection SelectedObj = null;
    Outlook.MailItem selMail = null;
    currentExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
    SelectedObj = currentExplorer.Selection; // <--COMException here

    I am getting a COMException (HRESULT: -2009989111) on the last line when nothing is selected in Outlook (i.e. Outlook starts, I click the button in the "add ins" tab which runs the previous code). It works fine when items are selected (I can get the selection and process each of its members), but obviously it might be the case that nothing is selected.

    I would have thought you could test for this by using:

    if(selectedObj.Count > 0)

    or test whether selectedObj itself it is null or not, but I cannot get that far. With nothing selected in the Outlook window I get an immediate exception. I could do a try ... catch on COMException but this seems less than ideal, does anyone know what I am doing wrong?

    Any help appreciated.

    Saturday, June 1, 2013 6:48 PM