Difference between revisions of "A 'hello world' VBA program"
(→Making our code more compact) |
|||
(18 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __FORCETOC__ | ||
== Creating a tab == | == Creating a tab == | ||
* Enter the code below in a standard VBA module | * Enter the code below in a standard VBA module | ||
Line 14: | Line 15: | ||
' Create a new tab | ' Create a new tab | ||
Dim myTab As rxTab | Dim myTab As rxTab | ||
− | Set myTab = | + | 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 27: | Line 30: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* Run the sub to create an empty tab labeled 'My First Tab' | * Run the sub to create an empty tab labeled 'My First Tab' | ||
− | *: [[image: EmptyTab.png]] | + | *: [[image: EmptyTab.png|link=]] |
== Code Analysis == | == Code Analysis == | ||
− | <syntaxhighlight lang="vb"> | + | <syntaxhighlight lang="vb" line start="3"> |
+ | ' Get a reference to the default rxCustomUI instance | ||
Dim myCustomUI As rxCustomUI | Dim myCustomUI As rxCustomUI | ||
Set myCustomUI = rxCustomUI.defaultInstance | Set myCustomUI = rxCustomUI.defaultInstance | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | [[rxCustomUI]] is at the top of the object model hierarchy. Here, we are holding on to the default rxCustomUI instance | + | [[rxCustomUI]] is at the top of the object model hierarchy. Here, we are holding on to the default rxCustomUI instance of the current session. |
+ | <syntaxhighlight lang="vb" line start="7"> | ||
+ | ' Get a reference to the rxRibbon object of our rxCustomUI instance | ||
+ | Dim myRibbon As rxRibbon | ||
+ | Set myRibbon = myCustomUI.ribbon | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Each rxCustomUI object owns a unique [[rxRibbon]] object. Here, we are holding on to the rxRibbon object of myCustomUI | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="vb" line start="11"> | ||
+ | ' Create a new tab | ||
+ | Dim myTab As rxTab | ||
+ | Set myTab = New rxTab | ||
+ | |||
+ | ' Give the new tab a label | ||
+ | myTab.Label = "My First Tab" | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Here we create a new [[rxTab]] object and give it a label | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="vb" line start="18"> | ||
+ | ' 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" line start="22"> | ||
+ | ' Render the UI | ||
+ | myCustomUI.Refresh | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | To ensure optimal performance, UI updates always take place in two steps: | ||
+ | # Update the target [[rxCustomUI]] state, which we have done above | ||
+ | # Render the updated UI, which we are doing here | ||
+ | |||
+ | == Remarks == | ||
+ | This sub will create a new empty tab every time it is run. We can prevent that from happening by adding a call to rxCustomUI.clear before we start updating its state, which resets the object's state (see [[#Making our code more compact]]). | ||
+ | To explicitly reset the state of the default rxCustomUI, you can execute the lines below in the immediate window: | ||
<syntaxhighlight lang="vb"> | <syntaxhighlight lang="vb"> | ||
− | + | rxCustomUI.defaultInstance.clear | |
+ | rxCustomUI.defaultInstance.refresh | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Making our code more compact == | ||
+ | The Ribbon Commander API has been specifically designed to take advantage of the 'With blocks' feature of VBA. All .add() methods return the added object, so the code of this example can be rewritten like so: | ||
+ | |||
+ | <syntaxhighlight lang="vb" line> | ||
+ | Public Sub CreateMyUI2() | ||
+ | |||
+ | With rxCustomUI.defaultInstance | ||
+ | ' Clear old state | ||
+ | .Clear | ||
+ | |||
+ | With .ribbon.tabs.Add(New rxTab) | ||
+ | .Label = "My First Tab" | ||
+ | End With | ||
+ | |||
+ | ' Render the UI | ||
+ | .Refresh | ||
+ | End With | ||
+ | |||
+ | End Sub | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 20:39, 19 October 2013
Creating a tab
- Enter the code below in a standard VBA module
Public Sub CreateMyUI()
' Get a reference to the default rxCustomUI instance
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.defaultInstance
' Get a reference to the rxRibbon object of our rxCustomUI instance
Dim myRibbon As rxRibbon
Set myRibbon = myCustomUI.ribbon
' Create a new tab
Dim myTab As rxTab
Set myTab = New rxTab
' Give the new tab a label
myTab.Label = "My First Tab"
' Add the new tab to myRibbon's tabs
myRibbon.tabs.Add myTab
' Render the UI
myCustomUI.Refresh
End Sub
- Run the sub to create an empty tab labeled 'My First Tab'
Code Analysis
' Get a reference to the default rxCustomUI instance
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 of the current 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, UI updates always take place in two steps:
- Update the target rxCustomUI state, which we have done above
- Render the updated UI, which we are doing here
Remarks
This sub will create a new empty tab every time it is run. We can prevent that from happening by adding a call to rxCustomUI.clear before we start updating its state, which resets the object's state (see #Making our code more compact). To explicitly reset the state of the default rxCustomUI, you can execute the lines below in the immediate window:
rxCustomUI.defaultInstance.clear rxCustomUI.defaultInstance.refresh
Making our code more compact
The Ribbon Commander API has been specifically designed to take advantage of the 'With blocks' feature of VBA. All .add() methods return the added object, so the code of this example can be rewritten like so:
Public Sub CreateMyUI2()
With rxCustomUI.defaultInstance
' Clear old state
.Clear
With .ribbon.tabs.Add(New rxTab)
.Label = "My First Tab"
End With
' Render the UI
.Refresh
End With
End Sub