In the previous blog, I created a COM add-in in managed C# code that customized the Ribbon UI regardless of which document was open. In this topic, I use the same customization XML and create the add-in in Visual Basic 6.0. The existing Ribbon UI in Word 2007 is modified by adding a custom tab, custom group, and button. When you click the button, a company name is inserted into the document. To get started, follow these steps:

1. Start Microsoft Visual Basic 6.0 and select Addin as the project type. This will add a designer class to the project (Connect) and a form (frmAddin).
2. In the Project Explorer window, open the Designer window by right-clicking Connect and then select View Object. Select Microsoft Word from the Application drop-down list.
3. In the Initial Load Behavior drop-down list, select Startup.
4. In the Project Explorer, right-click MyAdd and in the Property window change the name of the add-in to RibbonSampleVB.
5. Remove frmAddin from the project.
6. From the Project window, right-click the Connect item and select view code.
7. 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.
8. Add the following statement to the OnConnection method to obtain a reference to Word when the add-in loads:

   Set oWD = Application

9. 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 in the OnConnection procedure. Once you're satisfied that the add-is being loaded, you can remove the line and rebuild the project:

   MsgBox "My Addin started in " & Application.Name

10. Next, at the top of the code window, add the following statements to set aside memory for the application object we added earlier and to create a reference to the Ribbon UI interface:

   Dim oWD As Object
  
   Implements IRibbonExtensibility

11. Next, you'll implement the IRibbonExtensibilty interface's only member: GetCustomUI

   Public Function IRibbonExtensibility_GetCustomUI(ByVal RibbonID As String) As String
      IRibbonExtensibility_GetCustomUI = GetRibbonXML()
   End Function

This procedure calls the GetRibbonXML method that, as it's name implies, returns the customization XML to the GetCustomUI method which then gives it to the Ribbon UI to implement when the add-in loads.

12. Add the GetRibbonXML function. In this instance, the customization code is stored in a string variable that is returned to the GetCustomUI method:

   Public Function GetRibbonXML() As String
   Dim sRibbonXML As String

    sRibbonXML = "<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=""InsertCompanyName"" />" & _
                "</group >" & _
                "</tab>" & _
                "</tabs>" & _
                "</ribbon>" & _
                "</customUI>"
  
   GetRibbonXML = sRibbonXML
  
   End Function
 
13. Now, add the procedure that gets called when you click the button in the custom tab:

Public 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 = oWD.ActiveDocument.Range
   MyText = "Microsoft Corporation"
   ' Selection Example:
   'Selection.InsertBefore (MyText)
   ' Range Example: Inserts text at the beginning
   ' of the active document.
   MyRange.InsertBefore (MyText)
End Sub

14. To make sure the code compiles without error, on the Run menu, click Start with Full Compile.
15. 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 will register the add-in for you.
16. Start Word. You should see the "My Tab" tab to the right of the other tabs.
17. Click on the tab and then click the Insert Company Name button. The company name is inserted into the document.

If you have trouble loading the add-in or if the button doesn't work, you may need to do one of the following:

1. Go to the Trust Center to enable macros. Click the Microsoft Office Button (this is the round button in the upper left of the screen).
2. Click Trust Center, click Macro Settings, and then select the "Disable all macros with notification" and the "Trust access to the VBA project object model" options.
3. Close and reopen the document.

If you don’t see the My Tab tab when you start Word, you may need to add an entry to the registry. To do this, perform the following steps:

Caution: 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.

1. On the desktop click Start, click Run, and type regedit.
2. From the Registry tab, navigate to the following registry key for the add-in:

HKCU\Software\Microsoft\Office\Word\AddIns\RibbonXSampleVB.Connect

Note: 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 LoadBehavior DWord and set its value to 3.

And that's all there is to creating a COM Add-in to customize the Ribbon UI. In the next blog in this series, we'll look at additional properties of the Ribbon UI and XML markup you use to further customize the Ribbon UI.