Visually Indicating the State of a Command Bar Control

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Many menu commands or toolbar buttons are used to toggle the state of some part of an application from one condition to another. For example, in Microsoft® Office applications, the Bold button and the Align Left button will appear pressed in or not pressed in, depending on the formatting applied to text at the current selection. You can achieve this same effect with your custom command bar button controls by setting the State property to one of the msoButtonState constants.

Note   The State property is read-only for built-in command bar controls.

The following procedure shows how to explicit set the State property of a custom command bar button control:

Function CBCtlSetState(strCBarName As String, _
                          strCtlCaption As String) As Boolean
                     
   ' Set the State property of the strCtlCaption control
   ' on the strCBarName command bar. The State property is
   ' read-only for built-in controls, so if strCtlCaption
   ' is a built-in control, return False and exit the procedure.
   
   Dim ctlCBarControl As CommandBarControl
   
   On Error Resume Next
   
   Set ctlCBarControl = Application.CommandBars(strCBarName).Controls(strCtlCaption)
   
   If ctlCBarControl.BuiltIn = True Then
      CBCtlSetState = False
      Exit Function
   End If
   
   If ctlCBarControl.Type <> msoControlButton Then
      CBCtlSetState = False
      Exit Function
   End If
   
   CtlCBarControl. State =
   If C.State = MsoButtonDown Then
        C.State = MsoButtonUp
      Else If C.State = MsoButtonUp Then
        C.State = MsoButtonDown
      Else
        'State is mixed, leave it
   End If
   
   If Err = 0 Then
      CBCtlSetState = True
   Else
      CBCtlSetState = False
   End If
End Function

See Also

Working with Command Bars | Working with Command Bar Controls | Adding Controls to a Command Bar | Showing and Enabling Command Bar Controls