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

发布于 2024-11-27 00:22:51 字数 433 浏览 0 评论 0原文

我正在创建 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;
}

有谁知道这是怎么回事?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花伊自在美 2024-12-04 00:22:51

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

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

希望这有帮助。

The following link might provide you with some insight:

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

The "context" of the control gives you the corresponding Outlook object that you are customizing (for example an Inspector object). From there you'll need to reference the context object's CurrentItem property to get the MailItem.

For example,

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

Hopefully, this helps.

大姐,你呐 2024-12-04 00:22:51

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

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

有关更多详细信息,请访问此处。

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.

做个ˇ局外人 2024-12-04 00:22:51

当我无法弄清楚动态 ComObject 是什么时,我会使用它。

添加对 Microsoft.VisualBasic 的引用

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

只是需要它来完成与您几乎相同的事情,我的 IRibbonControl.Context 实际上也是一个选择,尽管它只选择了一个项目。

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.

一刻暧昧 2024-12-04 00:22:51

如果您想在 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;

在我的例子中,“全局”对我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文