Adding menus items to menus


Posted by Carl B on December 26, 2001 9:52 PM

I can add a menu to the menu bar, I can add menu items to that menu, I can add a menu as one of the items on that menu, I cannot add any menu items to the menu on that menu. Hope I didn't confuse anyone with that ( I confused myself)
This is what I have so far:

Private Sub Workbook_Open()
' Creates a new menu and adds menu items
Dim Cap(1 To 15)
Dim Mac(1 To 15)
Dim MenuName(1 To 15) As String


MenuName(1) = "About"
MenuName(2) = "&Update"
MenuName(3) = "Testing"
ActionName = "CommandButton1_Click"

Cap(1) = "Version 1.6"
Cap(2) = "Designed by "
Cap(3) = "For: "
Cap(4) = "Company Name"
Cap(5) = "Advance Timesheet One Pa&y Period"

Mac(1) = "CommandButton1_Click"

On Error Resume Next
' Delete the menu if it already exists
MenuBars(xlWorksheet).Menus(MenuName(1)).Delete
MenuBars(xlWorksheet).Menus(MenuName(2)).Delete
MenuBars(xlWorksheet).Menus(MenuName(3)).Delete

' Add the menus
' MenuBars(xlWorksheet).Menus.Add Caption:=MenuName(3)
MenuBars(xlWorksheet).Menus.Add Caption:=MenuName(2), Before:="Help"
MenuBars(xlWorksheet).Menus.Add Caption:=MenuName(1), Before:="Help"



' Add the menu items
With MenuBars(xlWorksheet).Menus(MenuName(1)).MenuItems
.Add Caption:=Cap(1)
.Add Caption:=Cap(2)
.Add Caption:=Cap(3)
.Add Caption:=Cap(4)
.AddMenu Caption:=MenuName(3)

End With


With MenuBars(xlWorksheet).Menus(MenuName(2)).MenuItems
.Add Caption:=Cap(5), OnAction:=Mac(1)

End With

End Sub

Posted by Gary Bailey on December 27, 2001 5:44 AM

Try

commandbars("about").Controls("testing").controls.add
commandbars("about").Controls("testing").controls(1).caption="You get the idea"

If I've got your problem right you need to access the controls collection of the control on the menu.

The VBA help is quite useful on the topic.

What version of Excel are you using? Is it 97 or later? If so menubars are considered a special type of commandbar and should be accessed that way - hence the MenuBars collection is a "hidden" member of the Excel object model.

Gary



Posted by Carl B on December 27, 2001 6:11 AM

I will try this, Thanks!
It is Office 2000 Pro