ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro attached to New Menu item on toolbar (https://www.excelbanter.com/excel-programming/412080-macro-attached-new-menu-item-toolbar.html)

steven

Macro attached to New Menu item on toolbar
 
I custom created a New Menu on the toolbar. It has an Assigned Macro. The
issue is if the user opens the file with the toolbar Menu item and then saves
the file somewhere else then the Assigned Macro follows it to the newly saved
file and I want to hold the macro at the original file.

Thank you for your help,

Steven

Bernie Deitrick

Macro attached to New Menu item on toolbar
 
Steven,

My customary advice it to create and destroy the commandbar through code
using the workbook's open
and close events. See below.

HTH,
Bernie
MS Excel MVP

In the workbook's Thisworkbook object code module, place the following code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
DeleteCommandbar
End Sub

Private Sub Workbook_Open()
CreateCommandbar
End Sub

In a regular code module, place the following:

Dim myBar As CommandBar
Dim myButton As CommandBarButton

Sub CreateCommandbar()

On Error Resume Next
DeleteCommandBar

Set myBar = Application.CommandBars.Add("My Bar")
With myBar
.Position = msoBarTop
.Visible = True
.Enabled = True
Set myButton = .Controls.Add(Type:=msoControlButton, ID:=23)
With myButton
.Caption = "Hello"
.Style = msoButtonIcon
.FaceId = 137
.Enabled = True
.OnAction = "SayHello"
End With
End With

End Sub

Sub DeleteCommandBar()
'Delete the commandbar if it already exists
On Error Resume Next
Application.CommandBars("My Bar").Delete
End Sub

Sub SayHello()
MsgBox "Hello there"
End Sub

You can add as many buttons or other menu items as you like, and assign
whatever macros you want.


"Steven" wrote in message
...
I custom created a New Menu on the toolbar. It has an Assigned Macro. The
issue is if the user opens the file with the toolbar Menu item and then
saves
the file somewhere else then the Assigned Macro follows it to the newly
saved
file and I want to hold the macro at the original file.

Thank you for your help,

Steven





All times are GMT +1. The time now is 10:38 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com