Example 3

This example is done in Word 2007 for a change. We are going to create an Extra tab to the Ribbon with a group that is visible in all documents and a second group that is only visible in all documents based on a particular template.

First you should open Word Options, Advanced, go to the General section and check Show add-in user interface errors. Now Word will warn you when an error occurs and give you an indication of its nature and location.

Part 1

Create a new document in Notepad and insert the following text:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:x="ns">
<ribbon startFromScratch="false">
<tabs>
<tab idQ="x:customTab" label="Extra">
   <group id="firstGroup" label="First Group">
      <button id="button1" label="First button" onAction="YouHitMe1"/>
      <button id="button2" label="Second button" onAction="YouHitMe2"/>
      <menu id="menu1" label="Menu example">
         <button id="menuButton1" label="First menu item" onAction="MenuChoice"/>
         <button id="menuButton2" label="Second menu item" onAction="MenuChoice"/>
      </menu>
   </group>
</tab></tabs></ribbon></customUI>
Save it to the desktop as ExtraTab.xml.

The only difference with the ExtraTab.xml from example1 is the xmlns:x="ns" element (xml name space). This allows you to manipulate the same tab from within several documents/templates. Instead of id="customTab" we now use idQ="x:customTab" in the tab element (Q stands for Qualified). The purpose of this is that the Extra tab will be created when it doesn't exist yet and when it already exists, only the new group(s) and command(s) will be added.

Make sure Word is closed.

In Windows Explorer, locate your Word normal template file Normal.dotm. Typically it resides in the C:\Documents and Settings\user\Application Data\Microsoft\Templates folder.

Of course it's wise to make a backup first.

Now rename it to Normal.dotm.zip. Double click to open it. Drag ExtraTab.xml to the root of the zip file.

Double click the _rels folder and drag the .rels file to the desktop. Right click it and select Edit. Just before the final </Relationships> element at the end, add the following text:

<Relationship Id="customUIRelID" Target="ExtraTab.xml"
 Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility"/>
This informs Word of the existence and purpose of ExtraTab.xml. Save the file and drag it to the _rels folder and click yes to overwrite the original file.

Remove the .zip extension from Normal.dotm.zip (so it's back to Normal.dotm) and Start Word. If you didn't make any mistakes, your Ribbon should have an additional Extra tab at the end with two buttons and a menu. To test the buttons you first have to add the following macros to a Module in the VB editor:

Option Explicit                                  
Sub YouHitMe1(Control As IRibbonControl)
   MsgBox "Hello, you clicked 'First button'"
End Sub                                          
Sub YouHitMe2(Control As IRibbonControl)
   MsgBox "Hello, you clicked 'Second button'"
End Sub                                          
Sub MenuChoice(control As IRibbonControl)
Select Case control.ID
   Case "menuButton1"
      MsgBox "Hello, you clicked 'First menu item'"
   Case "menuButton2"
      MsgBox "Hello, you clicked 'Second menu item'"
End Select
End Sub                                          
By clicking a button now, you get a message box indicating which button you clicked.

Part 2

Now we are going to create a new template. In Word, create a new blank document. In the VB editor insert a module and copy the following text in it:
Option Explicit                      
Sub ButtonA(control As IRibbonControl)
MsgBox "You clicked 'Button A'"
End Sub                              
Sub ButtonB(control As IRibbonControl)
MsgBox "You clicked 'Button B'"
End Sub                              
Save the file as Test.dotm (macro-enabled template) in the Templates folder.

Create a new document in Notepad and insert the following text:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:x="ns">
<ribbon startFromScratch="false">
<tabs>
<tab idQ="x:customTab" label="Extra">
   <group id="onlyTestTemplate" label="Only Test">
      <button id="buttonA" label="Button A" onAction="ButtonA"/>
      <button id="buttonB" label="Button B" onAction="ButtonB"/>
   </group>
</tab></tabs></ribbon></customUI>
Save it to the desktop as ExtraTab.xml.

You know the drill by now: rename Test.dotm to Test.dotm.zip, double click to open it in a folder window. Now you can see Word has already put a copy of ExtraTab.xml from the Normal.dotm template in it. The .rels file also contains already the relationship. So just drag your new ExtraTab.xml tot the root and click yes to overwrite it. Remove the .zip extension.

Open Word. In the Extra tab you will find your First group with the two buttons and a menu. Leave the Extra tab visible and open a new document based on the just created Test.dotm template. A second group Only Test appears. Try to create some more documents based on different templates. All of them will have the First Group but only those based on the Test template will have the second group.

Now you will of course want to modify the tab to suit you needs and change the macros to do something useful.

What about Excel?

The technique is similar. For commands you want to be available in all workbooks, the Part 1 example should be applied to an add-in file (.xlam) instead of Normal.dotm. Personal.xlsm doesn't work here because it is a hidden file and also hides the Ribbon customizations. An add-in is also hidden but it does expose the Ribbon customizations.

Create a new workbook and save it as an Excel add-in. In the Save As dialog type Personal for example as file name and select Excel Add-in (*.xlam) in the Save as type: drop down. Excel automatically selects the AddIns folder (typically C:\Documents and Settings\user\Application Data\Microsoft\AddIns\).

Go to Excel Options and select Add-Ins. In the Manage: drop down, select Excel Add-ins and click Go.... In the Add-Ins dialog, check Personal and click OK. Now perform the example procedure with this add-in file.

Part 2 can be applied to any Workbook you want to have particular commands to be available in. You even can use example 2 here, provided you use the name space element.