我有一个 Outlook 2007/2010 add-in,在其中我已成功向浏览器添加了 context-menu 按钮。该按钮本身可以正确显示并且可以正常工作,但是我无法将其放置在 context-menu 的 built-in 控件上方,因此始终将其添加到底部。我已经使用 VSTO 3.0 为 Outlook 2003 add-in 创建了相同的按钮,并且相同的代码创建了一个位于“打开”按钮上方上下文菜单顶部的按钮。
我的代码如下
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'参数无效。我怀疑问题是在将其他 built-in 控件添加到上下文菜单之前,已引发了 ItemContextMenuDisplay 事件,而在由 Explorer.CommandBars.OnUpdate 事件引发的方法中创建了 Outlook 2003 add-in 按钮,而该方法在 Explorer.CommandBars.OnUpdate 事件中不存在 VSTO 4.0 Explorer 对象。
是否可以在 VSTO 4.0 中为 Outlook 07/10 添加不在上下文菜单底部的按钮?
1 回答
在 Outlook 2003 和 2007 中,上下文菜单为 CommandBar-based,并使用上面提供的代码创建。在 Outlook 2010 中,上下文菜单现在为 Ribbon-based,并且通常使用 XML 进行声明。
来自在 Office 2010 中自定义上下文菜单:
Outlook 2010 支持 CommandBar-based 控件的向后兼容性,但有一些警告;无法定位控件可能是其中之一。
我的建议是让 add-in 检测正在运行的 Outlook 版本是 2003/2007 还是 2010,如果是后者,则创建 Ribbon-based 控件而不是 CommandBar-based 控件。您将需要研究如何相应地修改代码;例如,可以通过在
<button>
元素中声明insertBeforeMso
属性来执行定位。P.S。我鼓励您考虑切换到商用 third-party 产品Add-in Express for Microsoft Office 和.NET以扩展 Office 应用程序的用户界面;它大大简化了 VSTO 上的过程。您仍然需要创建单独的
ADXContextMenu
(CommandBar-based)和AdxRibbonContextMenu
(Ribbon-based),但是该过程几乎可以完全使用直观的视觉设计器完成。