Enable/Disable ribbon button commands

DCrake

Remembered
Local time
Today, 08:05
Joined
Jun 8, 2005
Messages
8,626
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.
 
Hi

To change at runtime, use the attribute getEnabled.

See article below on how to configure the ribbon. It has a video lesson showing in more detail.

ribbon01(dot)com/tutoriais/tuto10eng.asp?id=1#inicio

Success
 
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...
Code:
  <button id="cmdRibbons" label="Ribbons" imageMso="TableAutoFormat" onAction="RibbonClick" [COLOR="DarkRed"]getEnabled="ToolEnabled"[/COLOR] />
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'
Code:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" [COLOR="DarkRed"]onLoad="OnMainLoad"[/COLOR] >
This might look like...
Code:
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
 
As I expected it is not such a straight forward exercise to set up initally but easy once set up.
Thanks
 

Users who are viewing this thread

Back
Top Bottom