none
Outlook Ribbon Load Inspector.CurrentItem is null RRS feed

  • Question

  • Overview

    I have an Outlook add-in created with VSTO. The add-in has a single ribbon (visual designer) for theMail.Compose ribbon type. The ribbon tab ControlIdType is set to "Custom". The only code in the add-in other than designer code is the following Load handler for the ribbon.this.Context.CurrentItem is unexpectedly returning null.

    Code

    private void RibbonComposeMail_Load(object sender, RibbonUIEventArgs e)
    {
        try
        {
            var inspector = this.Context as Outlook.Inspector;
            if (inspector == null)
            {
                throw new ApplicationException("Fail - Step 1");
            }
    
            var currentMailItem = inspector.CurrentItem as Outlook.MailItem;
            if (currentMailItem == null)
            {
                throw new ApplicationException("Fail - Step 2");
            }
    
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    Steps

    1. Open draft email. Ribbon loads OK.
    2. Open email from inbox.
    3. Open same draft email. Ribbon fails at step 2, inspector.CurrentItem is null.

    Notes

    • I've tested this in Outlook 2007, 2010 and 2013, with an Outlook 2007 and 2010 add-in created in VS2010, and Outlook 2010 add-in created in VS2012. All behave the same.
    • Repeatedly opening the draft email doesn't appear to cause the issue, an Email.Read inspector has to be opened in between.
    • The ribbon tab ControlidType matters. "Custom" will cause the issue, but the default option of "Office" doesn't exhibit the issue.
    • Flipping the scenario on its head and setting the ribbon type to Mail.Read gives the same result, provided the sequence of opening is reversed to Inbox > Draft > Inbox (fail).
    • All possible permutations of calls to Marshal.ReleaseComObject on the inspector andcurrentMailItem objects makes no difference.
    Tuesday, August 27, 2013 9:32 PM