Results 1 to 3 of 3

Thread: [Excel 2003] Error when running sub in error handler

  1. #1
    Addicted Member
    Join Date
    Mar 09
    Posts
    157

    Question [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:
    1. Sub ActivateWB(WBName As String) 'macro that runs when toolbar button is pressed
    2.     Dim iResp As Integer
    3.     ' "'" & "modProcedures.ActivateWB """ & wkbk.Name & """'"
    4.     On Error GoTo e1
    5.     Workbooks(WBName).Activate
    6.     Exit Sub
    7. e1:
    8.     'workbook no longer exists or has had name change
    9.     'need to refresh bar
    10.     iResp = MsgBox("The workbook of that name is no longer available.", vbOKOnly + vbInformation, "Workbook Not Found:")
    11.     'Call RefreshMockTaskBar 'This is what errors out every time
    12. End Sub
    13.  
    14. Sub RefreshMockTaskBar()
    15.     Dim cbctrl As CommandBarControl
    16.     Dim cbMTB As CommandBar
    17.     Dim cbcNew As CommandBarControl
    18.     Dim sCBName As String
    19.     Dim wkbk As Variant
    20.     Dim bExists As Boolean
    21.    
    22.     sCBName = "MockTaskBar" 'my custom bar's name
    23.  
    24.     'Add a button for each open workbook, if not one already
    25.     For Each wkbk In Workbooks
    26.         If wkbk.Name <> "MockBar.xla" Then
    27.             Call AddButton(wkbk.Name, sCBName)
    28.         End If
    29.     Next wkbk
    30.    
    31.     Set cbMTB = CommandBars(sCBName)
    32.    
    33.     'Remove any buttons that are no longer applicable
    34.     For Each cbctrl In cbMTB.Controls
    35.         bExists = False
    36.         For Each wkbk In Workbooks
    37.             If cbctrl.Caption = wkbk.Name Then
    38.                 bExists = True
    39.             End If
    40.         Next wkbk
    41.         If bExists = False Then
    42.             Call RemoveButton(cbctrl.Caption, sCBName)
    43.         End If
    44.     Next cbctrl
    45.  
    46. End Sub
    47.  
    48. Sub AddButton(WBName As String, CBName As String)
    49.     Dim cbctrl As CommandBarControl
    50.     Dim MyBar As CommandBar
    51.     Dim cbcNew As CommandBarControl
    52.     Dim sCBName As String
    53.     Dim bExists As Boolean
    54.     Dim wkbk As Variant
    55.    
    56.     If WBName <> "MockBar.xla" Then
    57.         Call AddBar(CBName) 'add the bar if it doesn't already exist
    58.         Set MyBar = CommandBars(CBName) 'set the bar to a variable
    59.        
    60.         bExists = False
    61.         'check to see if a button exists for the wkbk
    62.         For Each cbctrl In MyBar.Controls
    63.             If cbctrl.Caption = WBName Then
    64.                 bExists = True
    65.             End If
    66.         Next cbctrl
    67.         'if no button exists, add a button
    68.         If bExists = False Then
    69.             Set cbcNew = MyBar.Controls.Add(Type:=msoControlButton)
    70.             With cbcNew
    71.                 .Style = msoButtonCaption
    72.                 .Caption = WBName
    73.                 .TooltipText = WBName
    74.                 .OnAction = "'" & "modProcedures.ActivateWB """ & .Caption & """'"
    75.             End With
    76.         End If
    77.     End If
    78. End Sub
    79.  
    80. Sub RemoveButton(WBName As String, CBName As String)
    81.     Dim cbar As CommandBar
    82.     Dim cbctrl As CommandBarControl
    83.     Dim MyBar As CommandBar
    84.     Dim bExists As Boolean
    85.     Dim wkbk As Variant
    86.     Dim iIndex As Long
    87.    
    88.     Call AddBar(CBName) 'add the bar if it doesn't already exist
    89.     Set MyBar = CommandBars(CBName) 'set the bar to a variable
    90.    
    91.     bExists = False
    92.     'check to see if a button exists for the wkbk & delete
    93.     For Each cbctrl In MyBar.Controls
    94.         If cbctrl.Caption = WBName Then
    95.             bExists = True
    96.             cbctrl.Delete
    97.         End If
    98.     Next cbctrl
    99. End Sub
    Attached Files Attached Files

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,520

    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

  3. #3
    Addicted Member
    Join Date
    Mar 09
    Posts
    157

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •