klav,
I am a novice in this area so take this simply as a working example but very
likely not the best solution.
The following XML creates a Tab "Practice Tab" with a "Practive Group" and a
single command button:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customUI xmlns="
http://schemas.microsoft.com/office/2006/01/customui"
onLoad="Main.Onload">
<ribbon>
<tabs>
<tab id="myTab1" label="Practice Tab" getVisible="Main.getVisible">
<group id="CntrGrp" label="Practice Group">
<button id="myBtn1" getLabel="Main.getLable" size="normal"
getEnabled="Main.getEnabled" onAction="Main.SampleMacro"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
In this example, the tab should only show if the IP is within a table. The
command button should be enabled if the table is uniform (the label should
be "Uniform") and disabled if the table is not uniform (the label should be
"Non-Uniform").
My understanding is that you need the "Onload" callback in order to create a
Ribbon obect in your VBA project.
As the ribbonX is loaded a Ribbon object is created in the project and the
initial state of the controls are determined based on the callbacks. These
values are stored in a cache.
The callbacks are located in a document module named Main:
Option Explicit
Private myDynamicMenu As myClass1
Public myRibbon As IRibbonUI
Sub AutoOpen()
Set myDynamicMenu = New myClass1
End Sub
Sub OnLoad(Ribbon As IRibbonUI)
Set myRibbon = Ribbon
End Sub
Private Sub getEnabled(control As iRibbonControl, ByRef enabled)
If control.ID = "myBtn1" Then
enabled = Selection.Tables(1).Uniform
End If
End Sub
Private Sub getLable(control As iRibbonControl, ByRef label)
If control.ID = "myBtn1" Then
If Selection.Tables(1).Uniform Then
label = "Uniform"
Else
label = "Non-Uniform"
End If
End If
End Sub
Private Sub getVisible(control As iRibbonControl, ByRef visible)
If control.ID = "myTab1" Then
visible = Selection.Information(wdWithInTable)
End If
End Sub
Sub SampleMacro(ByVal iRibbonControl)
MsgBox Selection.Tables(1).Columns.Count
End Sub
Now in order to change the values in the cache the controls must be
invalidated. To do this in this example, I used a Class module
WindowSelectionChange event:
The project contains a Class module named myClass1:
Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_WindowSelectionChange(ByVal oSel As Selection)
myRibbon.InvalidateControl ("myTab1")
myRibbon.InvalidateControl ("myBtn1")
End Sub
Each time the userd moves the selection (i.e., clicks in or out of a table)
the event fires and invalidates the two controls (you can also invalide the
whole ribbon). This forces Word to go out and look for the value of these
controls again.
I hope this helps.