繁体   English   中英

如何从 Outlook 功能区上下文菜单中获取当前邮件项目

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

我正在创建一个 Outlook 2010 加载项,并在我的功能区中为 idMso="contextMenuMailItem" 添加了一个上下文菜单。 单击时,我想删除一个类别,但在单击事件处理程序中,当我将 ctl.Context 转换为 MailItem 时,它始终为空。

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

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

以下链接可能会为您提供一些见解:

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

只是因为几乎和你一样的事情需要它,我的 IRibbonControl.Context 实际上也是一个选择,尽管它只选择了一个项目。

如果您想在 Ribbon.cs 中引用 TheAddin,也许您还可以考虑使用“Globals”。

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

Outlook

 - ThisAddin.cs

Ribbon1.cs

在“ThisAddin.cs”中声明一个公共 MailItem 并将邮件项目分配给它:

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

然后在 Ribbon1.cs 中使用“Globals”访问它

MailItem item=Globals.ThisAddin.myMail;

就我而言,“全局变量”对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM