Office 2007 introduced Word documents that are actually nothing more than zipped (compressed)
XML. In fact, you can open a
docm file like any other
zip archive in Windows simply by changing the extension to
zip. Modifying the ribbon is as simple as editing a few files within the archive.
The steps below will work for the other Office file types as well.
To register a custom ribbon, edit
\_rels\.rels, which acts like a manifest. The
.rels file in a brand-new Word documents looks like this on my system:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> </Relationships>
To override or extend the default ribbon, insert a new relationship tag using the following values:
- Id: a unique string that identifies the relationship. (Go wild.)
- Type: use
The new .rels file might look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml" /> <!-- ...snip... --> <Relationship Id="myRibbon" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="/custom/myRibbon.xml" /> </Relationships>
In this example, the ribbon customizations live in a file at
Here is a sample custom ribbon, with some commentary to help you understand what’s going on:
<?xml version="1.0" encoding="utf-8" ?> <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"> <ribbon startFromScratch="true"> <!-- remove the default ribbon entirely --> <tabs> <!-- you can have multiple tabs, but there is only one tab in this example --> <tab id="CustomTab" label="My Tab"> <group id="SimpleControls" label="This Labels the Group"> <!-- the tab, group, and button label values are presented to the user --> <button idMso="FileSaveAs" imageMso="FileSaveAs" size="large" label="Save As" /> <!-- call built-in actions with idMso... --> <button idMso="FilePrint" imageMso="FilePrint" size="large" label="Print" /> <!-- use built-in icons with imageMso... --> <button id="btnInsertText" imageMso="WordArtEditTextClassic" size="large" label="Insert Text" onAction="subInsertText" /> <!-- call your own functions with onAction... --> </group> </tab> </tabs> </ribbon> </customUI>
To call your own function, like with the last button in the example above, the file must be saved as a macro-enabled document. Those use file extensions that end in
m rather than
Here is some sample code for a message box that will be displayed whenever the final button is pressed:
Private Sub subInsertText(IRibbonControl As Variant) MsgBox IRibbonControl.ID & " was clicked. " End Sub
You’ll notice that the subroutine has a ribbon control object passed in, which contains, among other things, the id from the ribbon
XML file. This allows you to use a single function and switch to handle multiple related buttons. This can be a helpful way to organize your code.
Be sure to visit the MSDN resource page for the Office 2010 Ribbon. There you can find complete lists of available control IDs and icons, as well as more in-depth information on the various ways of modifying the ribbon.