How do I get the current mail item from Outlook ribbon context menu

The following link might provide you with some insight:

http://msdn.microsoft.com/en-us/library/ff863278.aspx

The "context" of the control gives you the corresponding Outlook object that you are customizing (for example an Inspector object). From there you'll need to reference the context object's CurrentItem property to get the MailItem.

For example,

public bool btnRemoveCategory_IsVisible(Office.IRibbonControl ctl)
{
    var item = ctl.Context as Inspector;
    var mailItem = item.CurrentItem as MailItem;
    if (item != null)
        return (item != null && HasMyCategory(item));
    else
        return false;
}

Hopefully, this helps.


I use this when I can't work out what a dynamic ComObject is.

Add a reference to Microsoft.VisualBasic

private void whatType(object obj)
{           
  System.Diagnostics.Debug.WriteLine(Microsoft.VisualBasic.Information.TypeName(obj));
}

Just needed it for almost the same thing as you, my IRibbonControl.Context was actually a Selection too despite it only being one item selected.


You can retrieve Mail Item after click event fired from context menu from selected mail item -

public bool btnRemoveCategory_IsVisible(Office.IRibbonControl ctl)
{
        Explorer explorer = Globals.ThisAddIn.app.ActiveExplorer();
            if (explorer != null && explorer.Selection != null && explorer.Selection.Count > 0)
            {
                object item = explorer.Selection[1];
                if (item is MailItem)
                {
                    MailItem mailItem = item as MailItem;
                }
        }
}

For more details visit here.