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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With