Add CommandBar with Menu Items

amdnVuser

Senior member
May 17, 2005
210
0
0
Can anyone tell me how to add a custom commandbar with associated sub-menu items in VBA (for Excel)? I've searched all over the Web and MSDN, and I can't seem to find anything that works. I'd ideally like for the code to work for Office 2000-2003 if possible. Any information would be greatly appreciated.
 

amdnVuser

Senior member
May 17, 2005
210
0
0
I forgot to mention that I finally figured this out in case anyone ever needs to do this. Here's the code:

Public Sub CreateTempMenu()
'Declare Variables
Dim cbpop As CommandBarControl
Dim cbctl As CommandBarControl
Dim cbsub As CommandBarControl

'Create a Popup Control on the Main Menu Bar
Set cbpop = Application.CommandBars("Worksheet Menu Bar"). _
Controls.Add(Type:=msoControlPopup, Temporary:=True)
cbpop.Caption = "Temp Menu"
cbpop.Visible = True

'Add a Button Control Menu Item
Set cbctl = cbpop.Controls.Add(Type:=msoControlButton)
cbctl.Visible = True

'Next is Required for Caption
cbctl.Style = msoButtonCaption
cbctl.Caption = "Hello!"

'Action to Perform (place custom macro, function, sub, etc. in the quotes)
cbctl.OnAction = "someMacroOrFunctionOrSub"
End Sub