Overview
I have an Outlook add-in created with VSTO. The add-in has a single ribbon (visual designer) for the Mail.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
- Open draft email. Ribbon loads OK.
- Open email from inbox.
- 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 theinspector
andcurrentMailItem
objects makes no difference.