Difference between revisions of "A 'hello world' VBA program"

From Ribbon Commander Documentation
Jump to: navigation, search
(Code Analysis)
Line 14: Line 14:
 
     ' Create a new tab
 
     ' Create a new tab
 
     Dim myTab As rxTab
 
     Dim myTab As rxTab
     Set myTab = myRibbon.tabs.Add(New rxTab)
+
     Set myTab = New rxTab
       
+
 
      
 
      
 
     ' Give the new tab a label
 
     ' Give the new tab a label
 
     myTab.Label = "My First Tab"
 
     myTab.Label = "My First Tab"
 
      
 
      
 +
    ' Add the new tab to myRibbon's tabs
 +
    myRibbon.tabs.Add myTab
 +
       
 
      
 
      
 
     ' Render the UI
 
     ' Render the UI
Line 44: Line 46:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Each rxCustomUI object owns a unique rxRibbon object. Here, we are holding on to the rxRibbon object of myCustomUI
+
Each rxCustomUI object owns a unique [[rxRibbon]] object. Here, we are holding on to the rxRibbon object of myCustomUI
  
  
 
<syntaxhighlight lang="vb">
 
<syntaxhighlight lang="vb">
' Get a reference to the rxRibbon object of our rxCustomUI instance
+
' Create a new tab
Dim myRibbon As rxRibbon
+
Dim myTab As rxTab
Set myRibbon = myCustomUI.ribbon
+
Set myTab = New rxTab
 +
   
 +
' Give the new tab a label
 +
myTab.Label = "My First Tab"
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Here we create a new [[rxTab]] object and give it a label
 +
 +
 +
<syntaxhighlight lang="vb">
 +
' Add the new tab to myRibbon's tabs
 +
myRibbon.tabs.Add myTab
 +
</syntaxhighlight>
 +
 +
Each [[rxRibbon]] object has a collection of [[rxTab]] objects (accessible through its .tabs property). Here we add the tab we created above to our ribbon's tabs
 +
 +
 +
<syntaxhighlight lang="vb">
 +
' Render the UI
 +
myCustomUI.Refresh
 +
</syntaxhighlight>
 +
 +
To ensure optimal performance, the UI update always takes place in two steps:
 +
# Update the target rxCustomUI state, which we have done above
 +
# Render the updated UI, which we are doing here

Revision as of 18:24, 10 March 2013

Creating a tab

  • Enter the code below in a standard VBA module
  1. Public Sub CreateMyUI()
  2.  
  3.     ' Get a reference to the default rxCustomUI instance
  4.     Dim myCustomUI As rxCustomUI
  5.     Set myCustomUI = rxCustomUI.defaultInstance
  6.  
  7.     ' Get a reference to the rxRibbon object of our rxCustomUI instance
  8.     Dim myRibbon As rxRibbon
  9.     Set myRibbon = myCustomUI.ribbon
  10.  
  11.     ' Create a new tab
  12.     Dim myTab As rxTab
  13.     Set myTab = New rxTab
  14.  
  15.     ' Give the new tab a label
  16.     myTab.Label = "My First Tab"
  17.  
  18.     ' Add the new tab to myRibbon's tabs
  19.     myRibbon.tabs.Add myTab
  20.  
  21.  
  22.     ' Render the UI
  23.     myCustomUI.Refresh
  24.  
  25. End Sub
  • Run the sub to create an empty tab labeled 'My First Tab'
    EmptyTab.png

Code Analysis

Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.defaultInstance

rxCustomUI is at the top of the object model hierarchy. Here, we are holding on to the default rxCustomUI instance for the current office application session.


' Get a reference to the rxRibbon object of our rxCustomUI instance
Dim myRibbon As rxRibbon
Set myRibbon = myCustomUI.ribbon

Each rxCustomUI object owns a unique rxRibbon object. Here, we are holding on to the rxRibbon object of myCustomUI


' Create a new tab
Dim myTab As rxTab
Set myTab = New rxTab
 
' Give the new tab a label
myTab.Label = "My First Tab"

Here we create a new rxTab object and give it a label


' Add the new tab to myRibbon's tabs
myRibbon.tabs.Add myTab

Each rxRibbon object has a collection of rxTab objects (accessible through its .tabs property). Here we add the tab we created above to our ribbon's tabs


' Render the UI
myCustomUI.Refresh

To ensure optimal performance, the UI update always takes place in two steps:

  1. Update the target rxCustomUI state, which we have done above
  2. Render the updated UI, which we are doing here