April 2006
Beginner
1114 pages
98h 16m
English
You can also add or remove commands on existing menus in code. For example, you might want to add Contact Us and About commands to the Help menu for a workbook add-in. To do so:
Get a reference to the menu toolbar.
Use the FindControl method to get a reference to the existing Contact and About items.
Modify those menu items.
Add new Contact Us and About items.
The following code demonstrates the preceding steps by changing the caption of the Contact Us menu item to Contact Microsoft, then adds new Contact and About items to the Help menu:
Sub ChangeHelpMenu( ) Dim cb As CommandBar, cpop As CommandBarPopup, cbtn As CommandBarButton Dim index As Integer ' Get the menu bar (CommandBar). Set cb = Application.CommandBars("Worksheet Menu Bar") ' Get the Help menu (control's ID is 30010) Set cpop = cb.FindControl(msoControlPopup, 30010) ' Get the Contact Us item (control's ID is 7903) Set cbtn = cb.FindControl(msoControlButton, 7903, , , True) ' Change the caption. cbtn.Caption = "&Contact Microsoft" index = cbtn.index ' Add a new Contact item. Set cbtn = cpop.Controls.Add(msoControlButton, , , index, True) cbtn.Caption = "Contact &Author" cbtn.OnAction = "ContactAuthor" ' Get the About item Set cbtn = cb.FindControl(msoControlButton, 927, , , True) index = cbtn.index cbtn.BeginGroup = False ' Add a new About item. Set cbtn = cpop.Controls.Add(msoControlButton, , , index, True) cbtn.Caption = "About &" & ThisWorkbook.Name cbtn.OnAction = "ShowAbout" cbtn.BeginGroup = ...Read now
Unlock full access