Add ControlButton to CommandBar
Sub AddNewMB()
Dim myCommandBar As CommandBar, myCommandBarCtl As CommandBarControl
Dim myCommandBarSubCtl As CommandBarControl
On Error GoTo AddNewMB_Err
Set myCommandBar = CommandBars.Add(Name:="Sample Menu Bar", Position:= _
msoBarTop, MenuBar:=True, Temporary:=False)
myCommandBar.Visible = True
myCommandBar.Protection = msoBarNoMove
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlPopup)
myCommandBarCtl.Caption = "Displa&y"
Set myCommandBarSubCtl = myCommandBarCtl.Controls.Add(Type:=msoControlButton)
With myCommandBarSubCtl
.Style = msoButtonIconAndCaption
.Caption = "E&nable ClickMe"
.FaceId = 59
.OnAction = "=MsgBox(""You clicked Enable ClickMe"")"
.Parameter = 1
.BeginGroup = True
End With
Set myCommandBarSubCtl = myCommandBarCtl.Controls.Add(Type:=msoControlButton)
With myCommandBarSubCtl
.Style = msoButtonIconAndCaption
.Caption = "Di&sable ClickMe"
.FaceId = 276
.OnAction = "=MsgBox(""You Disable ClickMe"")"
.Parameter = 2
.BeginGroup = True
End With
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
With myCommandBarCtl
.BeginGroup = True
.Caption = "&ClickMe"
.Style = msoButtonCaption
.OnAction = "=MsgBox(""You clicked ClickMe"")"
End With
Set myCommandBarCtl = myCommandBar.Controls.Add(Type:=msoControlButton)
With myCommandBarCtl
.BeginGroup = True
.Caption = "&Set Visibility Off"
.Style = msoButtonCaption
.OnAction = "=MsgBox(""You set visibility off"")"
End With
Exit Sub
AddNewMB_Err:
msgBox "Error " & Err.number & vbCr & Err.Description
Exit Sub
End Sub
Related examples in the same category