Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a menu with code

Status
Not open for further replies.

TomG1

Programmer
Apr 12, 2001
25
BE
Hi all,
how can i create an extra menu in the menu-bar with vba-code? I have tried to record a macro and then do al the things necessary but if I run the code it doesn't create a proper menu.

anyone?

thx
 

TomG1:

MenuBars are one of VBA's Hidden Objects and are supported only for backward compatibility. To view hidden objects in the Object Browser, right-click in the Object Browser window and click Show Hidden Members on the shortcut menu.

The following code will add a MenuBar to the right of the Help MenuItem and insert two items with a break between them.

Sub Menus()
With MenuBars("Worksheet")
.Reset
.Menus.Add ("&Menu")
With .Menus("&Menu")
.MenuItems.Add ("Macro&1"), "Macro1"
.MenuItems.Add ("-")
.MenuItems.Add ("Macro&2"), "Macro2"
End With
End With
End Sub

The & sets the ALT+Shorcut Key for that item. The "Macro1" and "Macro2" are the names of the macros to run when the MenuItem is selected. Please let me know if you have any questions.

Regards, LoNeRaVeR
 
Hi LoneRaver,

when i try to execute the code, he doesn't seem to know
"MenuBars". I have right-clicked in the object browser and have clicked show hidden members and added the code but it doesn't work. What have I done wrong?

Tom
 

TomG1:

In what application are you attempting to create the Menu (Access, Excel, Outlook, Word) and what version are you using (95, 97, 2000)?
LoNeRaVeR
 

TomG1:

I apologize. My code was written in Excel VBA, and it won't transfer. I'm not familiar with Word VBA. I know that you will use the CommandBars("Menu Bar") object, but I'm not sure how to add items within the Menu Bar. If I find something, I'll let you know.

Take care, LoNeRaVeR
 
Hi,

no need to search any further, I've found it

Tom
 

TomG1:

Please do share so others may learn from your findings. LoNeRaVeR
 
Hi,

this is the code

CommandBars("Menu Bar").Controls.Add Type:=msoControlPopup, Before:=11, Temporary:=True

CommandBars("Menu Bar").Controls(11).Tag = "Brieven"
CommandBars("Menu Bar").Controls(11).Caption = "Brieven"

Set helpmenu = CommandBars.FindControl _ Type:=msoControlPopup, Tag:="Brieven")

Set helpmenudrop = helpmenu.Control.CommandBar

helpmenudrop.Controls.Add Type:=msoControlButton, Temporary:=True

helpmenudrop.Controls.Add Type:=msoControlButton, Temporary:=True

helpmenudrop.Controls(1).Caption = "Offerte"
helpmenu.Controls(1).OnAction = "Macro1"
helpmenudrop.Controls(2).Caption = "Oplevering"
helpmenu.Controls(2).OnAction = "Macro2"

if you click on the menu, for example on "Offerte", Macro1 will be executed.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top