如何从Outlook ribbon上下文菜单中获取当前邮件项目?

本文关键字:项目 获取 Outlook ribbon 菜单 上下文 | 更新日期: 2023-09-27 18:04:06

我正在创建一个Outlook 2010插件,并为我的ribbon添加了一个上下文菜单idMso="contextMenuMailItem"。单击,我想删除一个类别,但在单击事件处理程序,当我转换ctl。Context到MailItem,它总是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;
}

有人知道这里发生了什么吗?谢谢!

如何从Outlook ribbon上下文菜单中获取当前邮件项目?

下面的链接可能会给你一些启发:

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

控件的"上下文"为您提供了您正在自定义的相应Outlook对象(例如Inspector对象)。从这里,您需要引用上下文对象的CurrentItem属性来获取MailItem。

例如,

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;
}

您可以在选中的邮件项目中单击上下文菜单中触发的事件后检索邮件项目-

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;
                }
        }
}

当我不知道什么是动态ComObject时,我使用这个

添加对Microsoft的引用。VisualBasic

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

我需要它来做几乎和你一样的事情,我的iriboncontrol。上下文实际上也是一个选择项,尽管它只被选中了一个项。

如果您想在Ribbon.cs中引用TheAddin,也许您也可以考虑使用" global "

例如,假设你在解决方案资源管理器中有以下文件:

Outlook
 - ThisAddin.cs
Ribbon1.cs

在' thisadd .cs'中声明一个公共MailItem,并将邮件项赋值给它:

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

然后在ribbon .cs中使用" global "访问它

MailItem item=Globals.ThisAddin.myMail;

在我的例子中,是"环球"。