4

我有一个 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 Explorer 对象中不存在的 OnUpdate 事件。

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

4

1 回答 1

3

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

自定义 Office 2010 中的上下文菜单

在 Microsoft Office 2010 之前,在 Microsoft Office Fluent Ribbon 用户界面 (UI) 中自定义上下文(右键单击)菜单的唯一方法是使用 CommandBars 解决方案。在 Office 2010 中,您可以自定义内置上下文菜单,就像您可以自定义功能区 UI 的其他组件一样。这种基于 XML 的上下文菜单可扩展性模型基于熟悉的功能区可扩展性模型。这意味着您可以使用当前用于自定义功能区 UI 的相同 XML 标记和回调。此外,通过功能区 UI 可扩展性启用上下文菜单自定义不会“破坏”以前编写的命令栏解决方案。

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

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

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

于 2012-05-27T09:11:19.093 回答