Partilhar via


How to: Add commands to shortcut menus

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This topic demonstrates how to add commands to a shortcut menu in an Office application by using a VSTO Add-in.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects. See Features available by Office application and project type.

To add commands to shortcut menus in Office

  1. Add a Ribbon XML item to a document-level or VSTO Add-in project. For more information, see How to: Get started customizing the Ribbon. In

  2. Solution Explorer, select ThisAddin.cs or ThisAddin.vb.

  3. On the menu bar, choose View > Code.

    The ThisAddin class file opens in the Code Editor.

  4. Add the following code to the ThisAddin class. This code overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.

    protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
          return new Ribbon1();
    }
    
    Protected Overrides Function CreateRibbonExtensibilityObject() As Microsoft.Office.Core.IRibbonExtensibility
        Return New Ribbon1()
    End Function
    
  5. In Solution Explorer, select the Ribbon XML file. By default, the Ribbon XML file is named Ribbon1.xml.

  6. On the menu bar, choose View > Code.

    The Ribbon xml file opens in the Code Editor.

  7. In the Code Editor, add XML that describes the shortcut menu and the control that you want to add to the shortcut menu.

    The following example adds a button, a menu, and a gallery control to the shortcut menu for a word document. The control ID of this shortcut menu is ContextMenuText. For a complete list of Office 2010 shortcut control ID's, see Office 2010 help files: Office fluent user interface control identifiers.

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
      <contextMenus>
        <contextMenu idMso="ContextMenuText">
          <button id="MyButton" label="My Button" insertBeforeMso="HyperlinkInsert" onAction="GetButtonID" />
          <menu id="MySubMenu" label="My Submenu" >
            <button id="MyButton2" label="Button on submenu" />
          </menu>
          <gallery id="galleryOne" label="My Gallery">
            <item id="item1" imageMso="HappyFace" />
            <item id="item2" imageMso="HappyFace" />
            <item id="item3" imageMso="HappyFace" />
            <item id="item4" imageMso="HappyFace" />
          </gallery>
        </contextMenu>
      </contextMenus>
    </customUI>
    
  8. In Solution Explorer, choose MyRibbon.cs or MyRibbon.vb.

  9. Add a callback method to the Ribbon1 class for each control that you want to handle.

    The following callback method handles the My Button button. This code adds a string to the active document at the current location of the curser.

    Public Sub GetButtonID(ByVal control As Office.IRibbonControl)
        Dim currentRange As Word.Range = Globals.ThisAddIn.Application.Selection.Range
        currentRange.Text = "This text was added by the context menu named My Button."
    End Sub
    
    public void GetButtonID(Office.IRibbonControl control)
    {
        Microsoft.Office.Interop.Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
        currentRange.Text = "This text was added by the context menu named My Button.";
    }
    

See also