CommandBarControl.Tag property (Office)

Gets or sets information about the CommandBarControl, such as data that can be used as an argument in procedures, or information that identifies the control. Read/write.

Note

The use of CommandBars in some Microsoft Office applications has been superseded by the new ribbon component of the Microsoft Office Fluent user interface. For more information, see Overview of the Office Fluent ribbon.

Syntax

expression.Tag

expression A variable that represents a CommandBarControl object.

Return value

String

Example

To avoid duplicate calls of the same class when triggered with events, define the Tag property unique to the events. The following example demonstrates this concept with two modules.

Public WithEvents oBtn As CommandBarButton 
 
Private Sub oBtn_click(ByVal ctrl As Office.CommandBarButton, CancelDefault As Boolean) 
    MsgBox "Clicked " & ctrl.Caption 
 
End Sub 
 
Dim oBtns As New Collection 
      
Sub Use_Tag() 
     
    Dim oEvt As CBtnEvent 
    Set oBtns = Nothing 
 
    For i = 1 To 5 
        Set oEvt = New CBtnEvent 
        Set oEvt.oBtn = Application.CommandBars("Worksheet Menu Bar").Controls.Add(msoControlButton) 
        With oEvt.oBtn 
            .Caption = "Btn" & i 
            .Style = msoButtonCaption 
            .Tag = "Hello" & i 
        End With 
        oBtns.Add oEvt 
    Next 
      
End Sub

This example sets the tag for the button on the custom command bar to Spelling Button and displays the tag in a message box.

CommandBars("Custom").Controls(1).Tag = "Spelling Button" 
MsgBox (CommandBars("Custom").Controls(1).Tag)

See also

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.