Using XML in VBA

From Ribbon Commander Documentation
Revision as of 12:33, 15 March 2013 by Rxdff15551 bb53 (Talk | contribs) (A more complete example)

Jump to: navigation, search

Introduction

Dynamic RibbonX 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

  1. Public Sub InstantiateButtonFromXML()
  2.  
  3.     ' Create a new button
  4.     Dim myButton As rxButton
  5.     Set myButton = New rxButton
  6.  
  7.     myButton.readXml "<button supertip=""My button supertip"" enabled=""true"" label=""My Button""/>"
  8.  
  9.     Debug.Print "supertip=[" & myButton.supertip & "], " & _
  10.                 "enabled=[" & (myButton.Enabled = rxTrue) & "], " & _
  11.                 "label=[" & myButton.Label & "]"
  12.  
  13. 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

  1. Public Sub CompleteExample()
  2.  
  3.     ' Create a new rxCustomUI
  4.     Dim myCustomUI As rxCustomUI
  5.     Set myCustomUI = rxCustomUI.Create("my_test_context")
  6.  
  7.     With myCustomUI
  8.         With .ribbon.tabs.Add(New rxTab)
  9.             .label = "My Tab"
  10.             .visible = rxTrue
  11.             With .groups.Add(New rxGroup)
  12.                 .label = "Group 1"
  13.                 With .buttons.Add(New rxButton)
  14.                     .label = "Button1"
  15.                     .onAction = myCustomUI.make_delegate("Callback1")
  16.                 End With
  17.                 With .buttons.Add(New rxButton)
  18.                     .label = "Button2"
  19.                     .getEnabled = myCustomUI.make_delegate("Callback2")
  20.                 End With
  21.             End With
  22.             With .groups.Add(New rxGroup)
  23.                 .label = "Group 2"
  24.                 With .buttons.Add(New rxButton)
  25.                     .label = "Button3"
  26.                     .getKeytip = myCustomUI.make_delegate("Callback3")
  27.                 End With
  28.                 With .buttons.Add(New rxButton)
  29.                     .label = "Button4"
  30.                     .getShowImage = myCustomUI.make_delegate("Callback4")
  31.                     .id = "button4"
  32.                 End With
  33.             End With
  34.  
  35.         End With
  36.  
  37.         ' Cache the xml state of 'myCustomUI' and print it out in the debug window
  38.         Dim sXml As String
  39.         sXml = .SerializeToXML
  40.  
  41.         Debug.Print sXml
  42.  
  43.     End With
  44.  
  45.     ' Use the cached state to instantiate a new rxCustomUI
  46.     Dim myCustomUI2 As rxCustomUI
  47.     Set myCustomUI2 = rxCustomUI.Create("another_context")
  48.  
  49.     myCustomUI2.populateFromXml sXml
  50.  
  51.     ' This will print 'XMLs are equal' if the states of the two objects are equal
  52.     Debug.Print IIf(myCustomUI.SerializeToXML = myCustomUI2.SerializeToXML, "XMLs are equal", "XMLs are not equal")
  53.  
  54. 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, 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.