Gary,
The code for your “StartChase” macro should be in a ‘normal’ module, not in a module ‘behind’ a sheet.
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Assign OnAction to button in Add-In?
Hi,
I’ve been having a good wrestle creating my first Excel add-in, and am stuck on what I hope is one last snag:
I’m using the following code to create a toolbar and button when Excel opens (the add-in will be in the startup folder) – this code is in the ThisWorkbook module of the add-in):
Private Sub Workbook_Open() Dim cbrAMG As CommandBar Dim ctlNewBtn As CommandBarButton Set cbrAMG = Application.CommandBars.Add(Name:="AMG_Chaser", _ Position:=msoBarTop, Temporary:=True) With cbrAMG .Visible = True Set ctlNewBtn = .Controls.Add(Type:=msoControlButton, Temporary:=True) With ctlNewBtn .FaceId = 2151 .OnAction = "StartChase" .Caption = "Send Chaser Msgs" End With End With End Sub
This code succeeds in creating the toolbar, the button and the tooltip. But when I click on the button to run the macro, I get an error message “The macro ‘AMG_ChaserAddIn_v1.xla!StartChase’ cannot be found.”
(The “StartChaser” macro is actually sitting, expectantly, in the code module behind Sheet1 of the add-in, waiting to be called.)
Any clues on how to programatically set the correct reference to this macro for the button’s OnAction property would be much appreciated – I’m guessing the difficulty has something to do with the way macros are available in add-ins but am drawing a blank at that point.
Thanks,
Gary
Gary,
Just forgot to mention this: I strongly advice you to add a few lines of code to the Workbook_Open event, and eventually even to the Workbook_BeforeClose event, to delete the toolbar you created.
Private Sub Workbook_Open()
Dim cbrAMG As CommandBar
Dim ctlNewBtn As CommandBarButton
On Error Resume Next
Application.CommandBars(“AMG_Chaser”).Delete
Set cbrAMG = Application.CommandBars.Add(Name:=”AMG_Chaser”, Position:=msoBarTop, Temporary:=True)
With cbrAMG
.Visible = True
Set ctlNewBtn = .Controls.Add(Type:=msoControlButton, Temporary:=True)
With ctlNewBtn
.FaceId = 2151
.OnAction = “StartChase”
.Caption = “Send Chaser Msgs”
End With
End With
End Sub
and
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.CommandBars(“AMG_Chaser”).Delete
End Sub
The before_close event will delete the toolbar when you uncheck the add-in.
Excel stores the toolbar settings to its xlb file when you quit Excel. When you start up Excel the next time, the toolbar will be there, so, the add-in will cause an error when trying to create a toolbar that already exists. You should delete the existing toolbar before you run the code that creates it.
Hans,
I somehow managed to miss your followup post a month ago, when you posted it ( ).
Just wanted to mention that I had already put code in to destroy the toolbar when the Excel app is closed.
The actual add-in was deployed a few weeks ago, so far no problems. Thanks again for your help in getting me set right!
Gary
I don’t want to find out the hard way so I thought I’d better ask:
By using the _Open event and _Close event to deploy and delete a custom toolbar, What would happen if the spreadsheet or Excel did not close properly, assuming that work had been saved? I’m thinking power loss, computer lock up, accidental re-boot, etc. Just trying to prevent an issue before it happens…Murphy will enter the picture.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications