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 Microsoft Office 2013 and Microsoft Office 2010. For more information, see Features Available by Office Application and Project Type.
To add commands to shortcut menus in Office
-
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
-
Solution Explorer, select ThisAddin.cs or ThisAddin.vb.
-
On the menu bar, choose View, Code.
The ThisAddin class file opens in the Code Editor.
-
Add the following code to the ThisAddin class. This code overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.
-
In Solution Explorer, select the Ribbon XML file. By default, the Ribbon XML file is named Ribbon1.xml.
-
On the menu bar, choose View, Code.
The Ribbon xml file opens in the Code Editor.
-
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> -
In Solution Explorer, choose MyRibbon.cs or MyRibbon.vb.
-
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.