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

Viewed 19570

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!

4 Answers

If you want to reference TheAddin in the Ribbon.cs, maybe you could also think to use "Globals".

For instance let say you have like following files in the solution explorer:

Outlook

 - ThisAddin.cs

Ribbon1.cs

Declare a public MailItem lin the 'ThisAddin.cs' and assign the mail item to it:

public MailItem myMail = null;
...
myMail=....

Then in the Ribbon1.cs access to it by using "Globals"

MailItem item=Globals.ThisAddin.myMail;

In my case, the "Globals" worked for me.

Related