Export (0) Print
Expand All

How to: Add Commands to Shortcut Menus

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

Applies to: The information in this topic applies to document-level projects and application-level projects for Office 2013 and Office 2010. 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 application-level 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 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 the 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
    
Show:
© 2014 Microsoft