Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am creating an Outlook 2010 add-in and have added a context menu to my ribbon for idMso="contextMenuMailItem". On click, I would like to remove a category but in the click event handler, when I cast ctl.Context to MailItem, it's always null.

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

Does anyone know what's going on here? Thanks!

like image 246
Keith Avatar asked Jul 28 '11 20:07

Keith


2 Answers

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.

like image 195
Aamol Avatar answered Sep 27 '22 18:09

Aamol


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.

like image 40
Matt Avatar answered Sep 27 '22 18:09

Matt