Sub menuing

T

Troubled User

I have added a "NewMenuItem" to the main commandbar. Below that I have added
two SubMenuItems with no assigned OnAction events.

Set Item = CommandBars(1).Controls("NewMenuItem").Controls.Add
Item.Caption = "&SubItem1"
Item.BeginGroup = False

Set Item = CommandBars(1).Controls("NewMenuItem").Controls.Add
Item.Caption = "&SubItem2"
Item.BeginGroup = False

What I want to do is be able to select the "SubItem1" and create multiple
menu items below it.

Thanks.
 
T

Tom Ogilvy

You want to do this in the same code that creates the SubItems or do you want
code to run when you select the created subitem?
 
T

Tom Ogilvy

Sub ABC()
Dim Menu1 As CommandBarPopup
Dim Item1 As CommandBarPopup
Dim Item2 As CommandBarPopup
Dim Item1Sub1 As CommandBarButton
Dim Item1Sub2 As CommandBarButton
Dim Item2Sub1 As CommandBarButton
Dim Item2Sub2 As CommandBarButton

On Error Resume Next
CommandBars(1).Controls("NewMenuItem").Delete
On Error GoTo 0
Set Menu1 = CommandBars(1).Controls.Add(Type:=msoControlPopup)
Menu1.Caption = "NewMenuItem"
Set Item1 = CommandBars(1).Controls("NewMenuItem" _
).Controls.Add(Type:=msoControlPopup)
Item1.Caption = "&SubItem1"
Item1.BeginGroup = False

Set Item2 = CommandBars(1).Controls("NewMenuItem" _
).Controls.Add(Type:=msoControlPopup)
Item2.Caption = "&SubItem2"
Item2.BeginGroup = False
Set Item1Sub1 = Item1.Controls.Add(Type:=msoControlButton)
Set Item1Sub2 = Item1.Controls.Add(Type:=msoControlButton)
Set Item2Sub1 = Item2.Controls.Add(Type:=msoControlButton)
Set Item2Sub2 = Item2.Controls.Add(Type:=msoControlButton)
Item1Sub1.Caption = "Item1Sub1"
Item1Sub2.Caption = "Item1Sub2"
Item2Sub1.Caption = "Item2Sub1"
Item2Sub2.Caption = "Item2Sub2"

End Sub

I just set the caption attributes, but you can add other attributes/onaction.
 
T

Troubled User

Thank you. Just what I wanted!

Tom Ogilvy said:
Sub ABC()
Dim Menu1 As CommandBarPopup
Dim Item1 As CommandBarPopup
Dim Item2 As CommandBarPopup
Dim Item1Sub1 As CommandBarButton
Dim Item1Sub2 As CommandBarButton
Dim Item2Sub1 As CommandBarButton
Dim Item2Sub2 As CommandBarButton

On Error Resume Next
CommandBars(1).Controls("NewMenuItem").Delete
On Error GoTo 0
Set Menu1 = CommandBars(1).Controls.Add(Type:=msoControlPopup)
Menu1.Caption = "NewMenuItem"
Set Item1 = CommandBars(1).Controls("NewMenuItem" _
).Controls.Add(Type:=msoControlPopup)
Item1.Caption = "&SubItem1"
Item1.BeginGroup = False

Set Item2 = CommandBars(1).Controls("NewMenuItem" _
).Controls.Add(Type:=msoControlPopup)
Item2.Caption = "&SubItem2"
Item2.BeginGroup = False
Set Item1Sub1 = Item1.Controls.Add(Type:=msoControlButton)
Set Item1Sub2 = Item1.Controls.Add(Type:=msoControlButton)
Set Item2Sub1 = Item2.Controls.Add(Type:=msoControlButton)
Set Item2Sub2 = Item2.Controls.Add(Type:=msoControlButton)
Item1Sub1.Caption = "Item1Sub1"
Item1Sub2.Caption = "Item1Sub2"
Item2Sub1.Caption = "Item2Sub1"
Item2Sub2.Caption = "Item2Sub2"

End Sub

I just set the caption attributes, but you can add other attributes/onaction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top