Ribbon (XML) is a powerful object which provides full control over existing Office Ribbon UI and customization.
The Ribbon (XML) item enables you to customize a Ribbon by using XML. Use the Ribbon (XML) item if you want to customize the Ribbon in a way that is not supported by the Ribbon (Visual Designer) item.
Adding a Ribbon (XML) Item to a Project
You can add a Ribbon (XML) item to any Office project from the Add New Item dialog box. Visual Studio automatically adds the following files to your project:
- A Ribbon XML file. This file defines the Ribbon user interface (UI). Use this file to add UI elements such as tabs, groups, and controls. For details, see Ribbon XML File Reference later in this topic.
- A Ribbon code file. This file contains the Ribbon class. This class has the name that you specified for the Ribbon (XML) item in the Add New Item dialog box. Microsoft Office applications use an instance of this class to load the custom Ribbon.
Note: By default, these files add a custom group to the Add-Ins tab in the Ribbon.
Displaying the Custom Ribbon in a Microsoft Office Application
After you add a Ribbon (XML) item to your project, you must add code to the ThisAddin, ThisWorkbook, or ThisDocument class that overrides the CreateRibbonExtensibilityObject method and returns the Ribbon XML class to the Office application.
The following code example overrides the CreateRibbonExtensibilityObject method and returns a Ribbon XML class named MyRibbon.
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new MyRibbon();
}
Defining the Behavior of the Custom Ribbon
You can respond to user actions, such as clicking a button on the Ribbon, by creating callback methods. Callback methods resemble events in Windows Forms controls, but they are identified by an attribute in the XML of the UI element. You write methods in the Ribbon class, and a control calls the method that has the same name as the attribute value. For example, you can create a callback method that is called when a user clicks a button on the Ribbon. Two steps are required to create a callback method:
- Assign an attribute to a control in the Ribbon XML file that identifies a callback method in your code.
- Define the callback method in the Ribbon class.
Note: Outlook requires an additional step. For more information, see Customizing a Ribbon for Outlook.
Assigning Callback Methods to Controls
To assign a callback method to a control in the Ribbon XML file, add an attribute that specifies the type of the callback method and the name of the method. For example, the following element defines a toggle button that has an onAction callback method named OnToggleButton1.
<toggleButton id="toggleButton1" onAction="OnToggleButton1" />
onAction is called when the user performs the main task associated with a particular control. For example, the onAction callback method of a toggle button is called when the user clicks the button.
The method that you specify in the attribute can have any name. However, it must match the name of the method that you define in the Ribbon code file.
Defining Callback Methods
Define your callback methods in the Ribbon class in the Ribbon code file. A callback method has several requirements:
- It must be declared as public.
- Its name must match the name of a callback method that you assigned to a control in the Ribbon XML file.
- Its signature must match the signature of a type of callback method that is available for the associated Ribbon control.
For a complete list of the callback method signatures for Ribbon controls, see the technical article Customizing the Office (2007) Ribbon User Interface for Developers (Part 3 of 3). Visual Studio does not provide IntelliSense support for callback methods that you create in the Ribbon code file. If you create a callback method that does not match a valid signature, the code will compile, but nothing will occur when the user clicks the control.
All callback methods have a T:Microsoft.Office.Core.IRibbonControl parameter that represents the control that called the method. You can use this parameter to reuse the same callback method for multiple controls. The following code example demonstrates an onAction callback method that performs different tasks depending on which control the user clicks.
public void OnActionCallback(Office.IRibbonControl control, bool isPressed)
{
if (control.Id == "checkBox1")
{
MessageBox.Show("You clicked " + control.Id);
}
else
{
MessageBox.Show("You clicked a different control.");
}
}
Ribbon XML File Reference
You can define your custom Ribbon by adding elements and attributes to the Ribbon XML file. By default, the Ribbon XML file contains the following XML.
<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnLoad">
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="MyGroup"
label="My Group">
</group>
</tab>
</tabs>
</ribbon>
</customUI>
The following table describes the default elements in the Ribbon XML file.
Element | Description |
---|---|
customUI | Represents the custom Ribbon in the VSTO Add-in project. |
ribbon | Represents the Ribbon. |
tabs | Represents a set of Ribbon tabs. |
tab | Represents a single Ribbon tab. |
group | Represents a group of controls on the Ribbon tab. |
These elements have attributes that specify the appearance and behavior of the custom Ribbon. The following table describes the default attributes in the Ribbon XML file.
Attribute | Parent element | Description |
---|---|---|
onLoad | customUI | Identifies a method that is called when the application loads the Ribbon. |
idMso | tab | Identifies a built-in tab to display in the Ribbon. |
id | group | Identifies the group. |
label | group | Specifies the text that appears on the group. |
The default elements and attributes in the Ribbon XML file are a small subset of the elements and attributes that are available. For a complete list of the available elements and attributes.
Ribbon Class Reference
Visual Studio generates the Ribbon class in the Ribbon code file. Add the callback methods for controls on the Ribbon to this class. This class implements the Microsoft.Office.Core.IRibbonExtensibility interface.
The following table describes the default methods in this class.
Method | Description |
---|---|
GetCustomUI | Returns the contents of the Ribbon XML file. Microsoft Office applications call this method to obtain an XML string that defines the user interface of your custom Ribbon. This method implements the M:Microsoft.Office.Core.IRibbonExtensibility.GetCustomUI(System.String) method. Note: GetCustomUI should be implemented only to return the contents of the Ribbon XML file; it should not be used to initialize your VSTO Add-in. In particular, you should not try to display dialog boxes or other windows in your GetCustomUI implementation. Otherwise, the custom Ribbon might not behave correctly. If you have to run code that initializes your VSTO Add-in, add the code to the ThisAddIn_Startup event handler. |
OnLoad | Assigns the T:Microsoft.Office.Core.IRibbonControl parameter to the ribbon field. Microsoft Office applications call this method when they load the custom Ribbon. You can use this field to dynamically update the custom Ribbon. |
GetResourceText | Called by the GetCustomUI method to obtain the contents of the Ribbon XML file. |
Next>>How to: Add a Dialog Box Launcher to a Ribbon Group