Difference between revisions of "Using XML in VBA"

From Ribbon Commander Documentation
Jump to: navigation, search
(Created page with "== Introduction == Dynamic RibbonX controls are both * Serializable to XML * Instantiatable from XML == Serializing to XML == <syntaxhighlight lang="vb"> Public Sub Serializi...")
 
(Introduction)
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
Dynamic RibbonX controls are both
+
Ribbon Commander controls are both
 
* Serializable to XML
 
* Serializable to XML
 
* Instantiatable from XML
 
* Instantiatable from XML
Line 14: Line 14:
 
     ' Build up state
 
     ' Build up state
 
     With myButton
 
     With myButton
         .ID = "my_button"
+
         .label = "My Button"
        .Label = "My Button"
+
         .enabled = rxTrue
         .Enabled = rxTrue
+
        .Visible = rxTrue
+
        .keytip = "K"
+
        .ScreenTip = "My button screentip"
+
 
         .supertip = "My button supertip"
 
         .supertip = "My button supertip"
 
          
 
          
Line 29: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The following xml code is printed out to the debug window:
+
The VBA code above prints out the following XML code to the debug window:
 
<syntaxhighlight lang="xml">
 
<syntaxhighlight lang="xml">
<button screentip="My button screentip" supertip="My button supertip" enabled="true" label="My Button" visible="true" keytip="K" id="my_button"></button>
+
<button supertip="My button supertip" enabled="true" label="My Button"></button>
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== Instantiating from XML ==
 +
<syntaxhighlight lang="vb" line>
 +
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
 +
</syntaxhighlight>
 +
 +
The VBA code above prints out the following XML code to the debug window:
 +
<pre>supertip=[My button supertip], enabled=[True], label=[My Button]</pre>
 +
 +
== A more complete example ==
 +
<syntaxhighlight lang="vb" line>
 +
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
 +
</syntaxhighlight>
 +
 +
The VBA code above prints out
 +
 +
<syntaxhighlight lang="xml">
 +
<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>
 +
</syntaxhighlight>
 +
 +
and
 +
<pre>XMLs are equal</pre>
 +
 +
== 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 [[Method create|created by]] and associated with [[rxCustomUI]] objects.

Latest revision as of 20:47, 19 October 2013

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

  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 (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.