In the preceding example, you have set the menu items as pop-ups. However, what happens if you just want to have additional items on submenus? Let’s take a look at the following code:
Public Sub newSubMenu() Dim menuBar As CommandBar Dim newMenu As CommandBarControl Dim menuItem As CommandBarControl Dim subMenuItem As CommandBarControl Set menuBar = CommandBars.Add(menuBar:=True, Position:=msoBarTop, _ Name:="Sub Menu Bar", Temporary:=True) menuBar.Visible = True Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup) newMenu.Caption = "&First Menu" Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup) newMenu.Caption = "&Second Menu" Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup) newMenu.Caption = "&Third Menu" Set menuItem = newMenu.Controls.Add(Type:=msoControlButton ) With menuItem .Caption = "F&irst Sub" .FaceId = "356" .OnAction = "myTest" End With Set menuItem = newMenu.Controls.Add(Type:=msoControlButton) With menuItem .Caption = "S&econd Sub" .FaceId = "333" .OnAction = "otherTest" End With Set menuItem = newMenu.Controls.Add(Type:=msoControlPopup) menuItem.Caption = "Sub Menus" Set subMenuItem = menuItem.Controls.Add(Type:=msoControlButton) With subMenuItem .Caption = "Item 1" .FaceId = 321 .OnAction = "firstMacro" End With Set subMenuItem = menuItem.Controls.Add(Type:=msoControlButton) With subMenuItem .Caption = "Item 2" .FaceId = 432 .OnAction = "secondMacro" End With End Sub
As you can see, we did a bit of modular construction here. You add menu items to the menu bar. Then you add submenu items to each of the menu items located on the menu bar.
As you can see in the code, it is a very repetitive pattern that can easily be extended to add even more levels of submenus. As a matter of fact, as a little experiment, why don’t you try to add another level under the subMenuItems.