How to: Customize the Ribbon UI by using a Visual Basic COM Add-in
The Ribbon user interface (UI) in the 2007 release of Microsoft® Office suites offers users a flexible way to work with Office applications. Ribbon Extensibility (RibbonX) uses text-based, declarative XML markup that simplifies creating and customizing the Ribbon UI. This sample demonstrates how to add create a custom Ribbon UI that appears in the Office application regardless of the document that is open. In the following steps, you create application-level customizations using a managed COM add-in which we create in Microsoft Visual Studio® 2005 using Microsoft Visual Basic® 6.0. The project adds a custom tab, a custom group, and button to the Ribbon UI that when clicked, inserts a company name into a Word 2007 document. You will:
Create a COM add-in project in Microsoft Visual Basic® 6.0 that works with Word 2007.
Add a reference to the IRibbonExtensibility interface.
Implement the only member of the interface: GetCustomUI.
Add the XML customization markup code.
Add the subroutine that is called when the button is clicked.
In this step you create a COM add-in by using an add-in designer.
Start Microsoft Visual Basic® 6.0 and select Addin as the project type. This adds a designer class to the project (Connect) and a
In the Project Explorer window, open the Designer window by right-clicking Connect and then select View Object. Select Microsoft Word from the Applicationlist.
In the Initial Load Behavior list, select Startup.
In the Project Explorer, right-click MyAddin and in the Property window change the name of the add-in to RibbonSampleVB.
Remove frmAddin from the project.
From the Project window, right-click the Connect item and select view code.
Remove all of the code in the designer's code window. This code works for Visual Basic add-ins but not Microsoft Office add-ins.
Add the following statement to the OnConnection method to obtain a reference to Word when the add-in loads:
Set oWD = Application
This step is optional however if you want to be notified that your add-in is actually loading when you start Word, you can add the following statement to the OnConnection procedure. Once you're satisfied that the add-is being loaded, you can remove the line and rebuild the project:
Add the procedure that gets called when you click the custom button:
Public Sub InsertCompanyName(ByVal control As IRibbonControl)
' Inserts the specified text at the beginning of a range.
Dim MyText As String
Dim MyRange As Object
Set MyRange = oWD.ActiveDocument.Range
MyText = "Microsoft Corporation"
' Inserts text at the beginning
' of the active document.
This procedure creates a reference to a range in the active document and then inserts the company name text into the range.
To make sure the code compiles without error, on the Run menu, click Start with Full Compile.
Once the code compiles without error, save the project and create the RibbonSampleVB.dll by clicking the File menu and then clicking Make RibbonSampleVB.dll.... The designer registers the add-in for
Start Word. You should see the My Tab tab to the right of the other tabs.
Click the tab and then click Insert Company Name. The company name is inserted into the document.
If you do not see the My Tab tab when you start Word, you may need to add an entry to the Microsoft Windows® registry. To do this, perform the following steps:
The next few steps contain information about modifying the registry. Before you modify the registry, be sure to back it up and
make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the
registry, search for the following article in the Microsoft Knowledge Base: 256986 Description of the Microsoft Windows Registry.
On the desktop, click Start, click Run, and type regedit.
From the Registry tab, navigate to the following registry key for the add-in: HKCU\Software\Microsoft\Office\Word\AddIns\RibbonXSampleVB.Connect
If the RibbonXSampleVB.Connect key does not exist, you can create it. To do so, right-click the Addins folder, point to New, and then
click Key. Name the key RibbonXSampleVB.Connect. Add a LoadBehaviorDWord and set its value to 3.