Creating an rxCustomUI object with global dispatch scope in VBA
Contents
Prerequisites
We recommend you go through Creating our first button in VBA before getting into this example.
Background
In previous examples we worked with the default rxCustomUI instance of the current session. This is fine for example code, but in real life it is preferable that each add-in/application creates its own context (or virtual add-in) to isolate its UI from other add-ins/applications.
The framework currently supports up to 1000 contexts/session so there are enough contexts to go around!
In VBA you can create rxCustomUI objects with two types of dispatch scopes (see rxCustomUI.create, make_delegate):
- Global dispatch. Intended to be used with VBA standard modules. Delegates created by the rxCustomUI object dispatch to public functions in standard VBA modules. The rxCustomUI object returned by the static function rxCustomUI.defaultInstance has global dispatch.
- Local context. Intended to be used with VBA classes. Delegates created by the rxCustomUI object dispatch to public methods of a user-supplied dispatch object, usually the owning class.
Creating a new global context with a check box
- Enter the code below in a standard module
Public Sub GlobalContextUI()
' Create an rxCustomUI in a global context
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.create("my_sample_context", "My Global Context", DispatchScope_global)
With myCustomUI
.Clear
With .ribbon.tabs.Add(New rxTab)
.Label = "Global Ctx Tab"
With .groups.Add(New rxGroup)
.Label = "Global Ctx Group"
With .CheckBoxes.Add(New rxCheckBox)
.Label = "Global Ctx Checkbox"
.OnAction = myCustomUI.make_delegate("MyGlobalCtxCheckBoxCallback")
End With
End With
End With
.Refresh
End With
End Sub
Sub MyGlobalCtxCheckBoxCallback(ByVal control As DynamicRibbonX.IRibbonControl, pressed As Boolean)
MsgBox "Global Ctx Checkbox " & IIf(pressed, "clicked", "un-clicked")
End Sub
- Run sub GlobalContextUI to create a new check box labeled Global Ctx Checkbox in its own global context.
Code Analysis
The above code should be pretty much familiar (see Creating our first button in VBA, instead of a button we are using a check box in this example) apart from the lines below:
' Create an rxCustomUI in a global context
Dim myCustomUI As rxCustomUI
Set myCustomUI = rxCustomUI.create("my_sample_context", "My Global Context", DispatchScope_global)
Here we create a new rxCustomUI in a global context through rxCustoUI's static function create with the following arguments
- contextID As String = "my_sample_context". An id string that uniquely identifies this context. Other add-ins/applications can access this same context by creating a context with the same id.
- description As String = "My Global Context". This is the context's description. It appears when a user hovers over one of the context's controls:
- dispatchScope As DispatchScope = DispatchScope_global. The context's dispatch scope.