Using XML in VBA
From Ribbon Commander Documentation
Contents
Introduction
Ribbon Commander controls are both
- Serializable to XML
- Instantiatable from XML
Serializing to XML
Public Sub SerializingButton() ' Create a new button Dim myButton As rxButton Set myButton = New rxButton ' Build up state With myButton .label = "My Button" .enabled = rxTrue .supertip = "My button supertip" ' Serialize to XML Debug.Print .XML End With End Sub
The VBA code above prints out the following XML code to the debug window:
<button supertip="My button supertip" enabled="true" label="My Button"></button>
Instantiating from XML
Public Sub InstantiateButtonFromXML()
' Create a new button
Dim myButton As rxButton
Set myButton = New rxButton
myButton.readXml "<button supertip=""My button supertip"" enabled=""true"" label=""My Button""/>"
Debug.Print "supertip=[" & myButton.supertip & "], " & _
"enabled=[" & (myButton.Enabled = rxTrue) & "], " & _
"label=[" & myButton.Label & "]"
End Sub
The VBA code above prints out the following XML code to the debug window:
supertip=[My button supertip], enabled=[True], label=[My Button]
A more complete example
Public Sub CompleteExample()
' Create a new rxCustomUI
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.Create("my_test_context")
With myCustomUI
With .ribbon.tabs.Add(New rxTab)
.label = "My Tab"
.visible = rxTrue
With .groups.Add(New rxGroup)
.label = "Group 1"
With .buttons.Add(New rxButton)
.label = "Button1"
.onAction = myCustomUI.make_delegate("Callback1")
End With
With .buttons.Add(New rxButton)
.label = "Button2"
.getEnabled = myCustomUI.make_delegate("Callback2")
End With
End With
With .groups.Add(New rxGroup)
.label = "Group 2"
With .buttons.Add(New rxButton)
.label = "Button3"
.getKeytip = myCustomUI.make_delegate("Callback3")
End With
With .buttons.Add(New rxButton)
.label = "Button4"
.getShowImage = myCustomUI.make_delegate("Callback4")
.id = "button4"
End With
End With
End With
' Cache the xml state of 'myCustomUI' and print it out in the debug window
Dim sXml As String
sXml = .SerializeToXML
Debug.Print sXml
End With
' Use the cached state to instantiate a new rxCustomUI
Dim myCustomUI2 As rxCustomUI
Set myCustomUI2 = rxCustomUI.Create("another_context")
myCustomUI2.populateFromXml sXml
' This will print 'XMLs are equal' if the states of the two objects are equal
Debug.Print IIf(myCustomUI.SerializeToXML = myCustomUI2.SerializeToXML, "XMLs are equal", "XMLs are not equal")
End Sub
The VBA code above prints out
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"> <ribbon> <tabs> <tab id="RX4C5E51-2222-4D76-AEF5-97D7460E8FC9" visible="true" label="My Tab"> <group id="RX4F6096-0B31-42F9-B299-823510E5C304" label="Group 1"> <button label="Button1" id="RXE10CA5-9197-4FF7-A7BA-5ECEB4DF6D6C" onAction="Callback1"></button> <button label="Button2" id="RXE269D3-0CB2-4031-90D2-E91304BC5D8F" getEnabled="Callback2"></button> </group> <group id="RX6A4144-9F73-4307-8F26-B2B107764E08" label="Group 2"> <button label="Button3" id="RX8480FB-8F17-4489-AA16-76C4178826EC" getKeytip="Callback3"></button> <button label="Button4" id="button4" getShowImage="Callback4"></button> </group> </tab> </tabs> </ribbon> </customUI>
and
XMLs are equal
Remarks
- Notice that when controls don't explicitly get assigned ids (see line 31 of the VBA code), they get assigned automatic ids like RX4F6096-0B31-42F9-B299-823510E5C304 when they get connected (directly or indirectly) to an rxCustomUI object.
- rxCustomUI has methods serializeToXml and populateFromXml while all other controls have methods xml and readXml. serializeToXml and populateFromXml take delegates into account, while xml and readXml don't, since rxDelegate objects are created by and associated with rxCustomUI objects.