Customize the Office Fluent ribbon by using an Open XML formats file
The ribbon component of the Microsoft Office Fluent user interface gives users a flexible way to work with Office applications. Ribbon Extensibility (RibbonX) uses a simple, text-based, declarative XML markup to create and customize the ribbon.
The code example in this topic shows how to add custom components to the ribbon for a single document, as opposed to adding application-level customizations. In the following steps, you add a custom tab, a custom group, and a custom button to the existing ribbon in Word. You also implement a callback procedure for the button that inserts a company name into the document.
Create the customization file in any text editor and save the file with the name customUI.xml.
Add the following XML markup to the file and then close and save the file.
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"> <ribbon> <tabs> <tab id="CustomTab" label="My Tab"> <group id="SampleGroup" label="Sample Group"> <button id="Button" label="Insert Company Name" size="large" onAction="ThisDocument.InsertCompanyName" /> </group > </tab> </tabs> </ribbon> </customUI>
Create a folder on your desktop named customUI and copy the XML customization file to the folder.
Validate the XML markup with a custom schema.
This step is optional.
Create a document in Word and save it with the name RibbonSample.docm.
Open the Visual Basic Editor, add the following procedure to the ThisDocument code module, and then close and save the document.
Sub InsertCompanyName(ByVal control As IRibbonControl) ' Inserts the specified text at the beginning of a range or selection. Dim MyText As String Dim MyRange As Object Set MyRange = ActiveDocument.Range MyText = "Microsoft Corporation" ' Range Example: Inserts text at the beginning ' of the active document MyRange.InsertBefore (MyText) ' Selection Example: 'Selection.InsertBefore (MyText) End Sub
Add a .zip extension to the document file name and then double-click it to open the file.
Add the customization file to the container by dragging the customUI folder from the desktop to the .zip file.
Extract the .rels file to your desktop. A _rels folder that contains the .rels file is copied to your desktop.
Open the .rels file and add the following line between the last Relationship tag and the Relationships tag. This creates a relationship between the document file and the customization file.
<Relationship Id="someID" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml" />
Close and save the file.
Add the .rels folder back to the container file by dragging it from the desktop, overwriting the existing file.
Rename the document file to its original name by removing the .zip extension.
Open the document and notice that the ribbon now displays the My Tab tab.
Choose the tab and notice the Sample Group group with a button control.
Choose the button to insert the company name into the document.
- Overview of the Office Fluent ribbon
- Customize the Office Fluent ribbon by using a managed COM add-in
- Customize the Office Fluent ribbon by using a Visual Basic COM add-in
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.