When creating custom ribbons, at some points in the application I need to be able to enable and or disable the button commands. I know the syntax is enable = "false" or enable = "true", however How do you or can you do it in vba when the ribbon is active on the screen.
I think all the functionality for ribbons is handled via callback functions, so you define the 'getEnabled' element for a ribbon control using the name of the function to be called when the ribbon is redrawn. This is a control I use to open a form to manage ribbons in a USysRibbons table...
And since I don't want most users to be able to open this form, I only selectively enable it...
Code:
Sub ToolEnabled(Control As IRibbonControl, ByRef enabled)
Select Case Control.ID
Case "cmdRibbons"
enabled = Sys.CurrentUser.SecureStatus < 1
Case "cmdOtherControl"
enabled = [COLOR="Green"]'this routine is invoked by various ribbon controls[/COLOR]
Case Else
enabled = False
End Select
End Sub
But to invoke that ToolEnabled callback you need to call the Invalidate method of the IRibbonUI object, so you need a reference to it, so you need to hang onto it way back when it loads using: A Callback. In this case 'OnLoad'
Private m_ribbon as IRibbonUI
Sub OnMainLoad(Ribbon As IRibbonUI)
[COLOR="Green"] 'handles the OnLoad callback of the customUI element
'and saves the IRibbonUI object reference to m_ribbon[/COLOR]
Set m_ribbon = Ribbon
End Sub
Then I might make that ribbon object available system-wide with a property like ...
Code:
Public Property Get MainRibbon as IRibbonUI
[COLOR="Green"] 'global property that re-exposes the ribbon system-wide
'so consumers can call the IRibbonUI.Invalidate method as required
[/COLOR] Set MainRibbon = m_ribbon
End Property
Finally, in the current event of a form, I might want to enable/disable a ribbon control. In that case I do this ...
Code:
Private Sub Form_Current()
[COLOR="Green"] 'consumes the global object reference to the ribbon
'calls its invalidate method
'and while rebuilding the ribbon your ToolEnabled() method will be called
'to enable, or not, your control[/COLOR]
MainRibbon.Invalidate
End Sub