Add a Reference to the IRibbonExtensibility Interface
At the top of the code window, add the following statements to set aside memory for the Word Application object that you added earlier, and to create a reference to the ribbon.
Option Explicit
Dim oWD As Object
Implements IRibbonExtensibility
Implement the IRibbonExtensibility Interface
In this step, you implement the IRibbonExtensibilty interface's only member, GetCustomUI. Add the following procedure at the bottom of the code window.
Public Function IRibbonExtensibility_GetCustomUI(ByVal RibbonID As String) As String
IRibbonExtensibility_GetCustomUI = GetRibbonXML()
End Function
This procedure calls the GetRibbonXML method that, as its name implies, returns the customization XML to the GetCustomUI method which then adds it to the ribbon to implement when the add-in loads.
Retrieve the XML Customization Code
In this step, you add the GetRibbonXML function. Here, 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=""[URL unfurl="true"]http://schemas.microsoft.com/office/2006/01/customui""[/URL] >" & _
"<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
Add the Procedure That Is Called When You Click the Button
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.
MyRange.InsertBefore (MyText)
End Sub