[Excel 2003] Error when running sub in error handler
So, the project I'm working on is an add-in that basically adds a mock windows taskbar to the bottom of the Excel application. It creates a custom toolbar and adds buttons for each workbook that's open, that will allow you to activate the workbooks with the buttons. When new workbooks are opened, new buttons are added for those workbooks, and when workbooks are closed, their buttons are deleted. It also refreshes the taskbar when you switch between workbooks, to try and avoid, "the problem".
"The problem" --the only problem, really, with it-- is that if you use 'Save As' and re-name a file, the toolbar does not update. It keeps the old name of the file on the button, and the button no longer works. I've included an error handler for the button action macro (ActivateWB is the sub name) which, right now, just pops up a message box saying that workbook is no longer available.
I've tried to call my Refresh procedure within that error handler to fix the broken button, but it always errors out. But, if the error handler is triggered, the message box displays, and the procedures ends, and then I go into the Refresh procedure and manually run it, it works fine with no errors. I can't figure out what the problem is, so if anyone has any suggestions, do share!
Below are the procedures involved in "the problem", and attached is a copy of my full add-in.
vb Code:
Sub ActivateWB(WBName As String) 'macro that runs when toolbar button is pressed
Re: [Excel 2003] Error when running sub in error handler
as you do not say where you get the error, i presume it is when the button is being deleted
the error is because the button is still in pressed state, therefore cannot be deleted
workaround is, on error, to make the button not visible, it will be deleted next time the code is run anyway, so you will only ever have need to have 1 not visible button
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Re: [Excel 2003] Error when running sub in error handler
Yes, the error happens when it tries to delete the button in the Refresh procedure. I'll take a look into hiding the button, potentially within an error trap in the delete procedure... Thanks for the suggestion.