Custom Menu

P

Phillip Topping

How do I create a Custom Menu that appears in the Menu bar when a particular
file loads and disappears again when that file is closed?

TIA
Phillip
 
B

Bob Phillips

Phillip,

If you put code in the appropriate workbook open event, and delete it in the
close it will exist only for that workbook.

Here is an example of a building a commandbar on the fly when you open a
workbook. It adds a sub-menu to the Tools menu.

Private Sub Workbook_Open()
Dim oCb As CommandBar
Dim oCtl As CommandBarPopup
Dim oCtlBtn As CommandBarButton

Set oCb = Application.CommandBars("Worksheet Menu Bar")
With oCb
Set oCtl = .Controls("Tools").Controls.Add(Type:=msoControlPopup,
temporary:=True)
oCtl.Caption = "myButton"
With oCtl
Set oCtlBtn = .Controls.Add(Type:=msoControlButton,
temporary:=True)
oCtlBtn.Caption = "myMacroButton"
oCtlBtn.FaceId = 161
oCtlBtn.OnAction = "myMacro"
End With
End With
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim oCb As CommandBar

Set oCb = Application.CommandBars("Worksheet Menu Bar")
oCb.Controls("Tools").Controls("myButton").Delete
End Sub


To add this, go to the VB IDE (ALT-F11 from Excel), and in the explorer
pane, select your workbook. Then select the 'ThisWorkbook' object (it's in
Microsoft Excel Objects which might need expanding). Double-click the
'ThisWorkbook' and a code window will open up. Copy this code into there,
changing the caption and action to suit.

This is part of the workbook, and will only exist with the workbook, but
will be available to anyone who opens the workbook.
 
P

Phillip Topping

Hi Bob,
Thanks for this assistance also, once again, very helpful.

Regards
Phillip
 

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