How to position Outlook 2007/2010 VSTO Context Men

2019-04-10 19:52发布

I have an Outlook 2007/2010 add-in where I have successfully added a context-menu button to the explorer. The button itself is displayed correctly and working fine however I am unable to position it above the built-in controls on the context-menu, it is always added to the bottom. I have created the same button using VSTO 3.0 for an Outlook 2003 add-in and the same code creates a button that is at the top of the context menu above the 'Open' button.

My code is below

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

    }

I have tried playing around with the 'Before' parameter of the CommandBar.Controls.Add() method to no avail. I am suspecting the problem is that the ItemContextMenuDisplay event is being fired before the other built-in controls are added to the context menu, whereas the Outlook 2003 add-in button is being created in a method that is fired by the Explorer.CommandBars.OnUpdate event which doesn't exist in the VSTO 4.0 Explorer object.

Is it possible to add a button that isn't on the bottom of the context menu in VSTO 4.0 for Outlook 07/10?

1条回答
手持菜刀,她持情操
2楼-- · 2019-04-10 20:43

In Outlook 2003 and 2007, context menus were CommandBar-based, and created using code like the one you provided above. In Outlook 2010, context menus are now Ribbon-based, and typically declared using XML.

From 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 supports backwards compatibility for CommandBar-based controls, but with some caveats; the inability to position the controls is probably one of them.

My suggestion would be to have your add-in detect whether the running Outlook version is 2003/2007 or 2010 and, in case of the latter, create Ribbon-based controls instead of the CommandBar-based ones. You will need to investigate how to adapt your code accordingly; for example, positioning may be performing by declaring insertBeforeMso attributes in the <button> element.

P.S. I would encourage you to consider switching to the commercial third-party product Add-in Express for Microsoft Office and .NET for extending the UI of Office applications; it simplifies the process drastically over VSTO. You would still need to create a separate ADXContextMenu (CommandBar-based) and AdxRibbonContextMenu (Ribbon-based), but the process may be done almost entirely using intuitive visual designers.

查看更多
登录 后发表回答