c# - 如何定位 Outlook 2007/2010 VSTO 上下文菜单按钮?

标签 c# outlook ms-office vsto

我有一个 Outlook 2007/2010 加载项,我已经成功地向资源管理器添加了一个上下文菜单按钮。按钮本身显示正确且工作正常,但我无法将其放置在上下文菜单的内置控件上方,它始终添加到底部。我使用 VSTO 3.0 为 Outlook 2003 加载项创建了相同的按钮,并且相同的代码创建了一个位于上下文菜单顶部“打开”按钮上方的按钮。

我的代码在下面

 void Application_ItemContextMenuDisplay(CommandBar CommandBar, Selection Selection)
    {
        if (Selection.Count != 1) return;

        CommandBarControl rootButton = CommandBar.Controls.Add(MsoControlType.msoControlButton, Type.Missing, "Create Heat Call", 1, Type.Missing);

        CommandBarButton button = (CommandBarButton)rootButton;

        button.BeginGroup = true;
        button.Tag = "CreateHeatCall";
        button.Caption = "Create Heat Call";
        button.Style = MsoButtonStyle.msoButtonIconAndCaption;
        button.Visible = true;

        button.Picture = GetImage();
        button.Mask = GetImageMask();

        selection = Selection;

        ((CommandBarButton)rootButton).Click += new _CommandBarButtonEvents_ClickEventHandler(ThisAddIn_Click);

    }

我曾尝试使用 CommandBar.Controls.Add() 方法的“Before”参数但无济于事。我怀疑问题是 ItemContextMenuDisplay 事件在其他内置控件添加到上下文菜单之前被触发,而 Outlook 2003 加载项按钮是在 Explorer.CommandBars 触发的方法中创建的。 VSTO 4.0 资源管理器对象中不存在的 OnUpdate 事件。

是否可以在 VSTO 4.0 for Outlook 07/10 的上下文菜单底部添加一个按钮?

最佳答案

在 Outlook 2003 和 2007 中,上下文菜单是基于 CommandBar 的,并且是使用您上面提供的代码创建的。在 Outlook 2010 中,上下文菜单现在基于功能区,并且通常使用 XML 声明。

来自 Customizing Context Menus in Office 2010 :

Prior to Microsoft Office 2010, the only way to customize context (right-click) menus in the Microsoft Office Fluent Ribbon user interface (UI) was by using CommandBars solutions. In Office 2010, you can customize built-in context menus just as you can the other components of the Ribbon UI. This XML-based context menu extensibility model is based on the familiar Ribbon extensibility model. This means that you can use the same XML markup and callbacks that you currently use to customize the Ribbon UI. Additionally, enabling context menu customizations through Ribbon UI extensibility does not “break” previously written command bar solutions.

Outlook 2010 支持基于 CommandBar 的控件的向后兼容性,但有一些注意事项;无法定位控件可能是其中之一。

我的建议是让您的加载项检测正在运行的 Outlook 版本是 2003/2007 还是 2010,如果是后者,则创建基于 Ribbon 的控件而不是基于 CommandBar 的控件。您将需要研究如何相应地调整您的代码;例如,定位可以通过声明 insertBeforeMso 来执行。 <button> 中的属性元素。

附言我鼓励您考虑切换到商业第三方产品 Add-in Express for Microsoft Office and .NET用于扩展 Office 应用程序的 UI;它大大简化了 VSTO 的过程。您仍然需要创建一个单独的 ADXContextMenu (基于 CommandBar)和 AdxRibbonContextMenu (基于功能区),但该过程几乎可以完全使用直观的视觉设计器来完成。

关于c# - 如何定位 Outlook 2007/2010 VSTO 上下文菜单按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10771686/

相关文章:

c#-4.0 - 如何使用预先填充的附件打开Outlook新邮件窗口

c# - 仅在多个空格上拆分 C#

c# - 自定义 Unity EditorWindow 忘记了选定的游戏对象/组件

excel - 添加超链接(Web)到 Outlook 电子邮件

正则表达式在outlook vba中提取文本和数字

excel - 如何覆盖网站安全证书以消除错误?

java - 使用相同的 API 编写 Word 和 PDF 文档

c# - 为多个应用程序创建 VSTO 插件

C# 如何使重复输入重叠?

c# - .NET:检查 URL 的响应状态代码?