Sub ActivateWB(WBName As String) 'macro that runs when toolbar button is pressed
Dim iResp As Integer
' "'" & "modProcedures.ActivateWB """ & wkbk.Name & """'"
On Error GoTo e1
Workbooks(WBName).Activate
Exit Sub
e1:
'workbook no longer exists or has had name change
'need to refresh bar
iResp = MsgBox("The workbook of that name is no longer available.", vbOKOnly + vbInformation, "Workbook Not Found:")
'Call RefreshMockTaskBar 'This is what errors out every time
End Sub
Sub RefreshMockTaskBar()
Dim cbctrl As CommandBarControl
Dim cbMTB As CommandBar
Dim cbcNew As CommandBarControl
Dim sCBName As String
Dim wkbk As Variant
Dim bExists As Boolean
sCBName = "MockTaskBar" 'my custom bar's name
'Add a button for each open workbook, if not one already
For Each wkbk In Workbooks
If wkbk.Name <> "MockBar.xla" Then
Call AddButton(wkbk.Name, sCBName)
End If
Next wkbk
Set cbMTB = CommandBars(sCBName)
'Remove any buttons that are no longer applicable
For Each cbctrl In cbMTB.Controls
bExists = False
For Each wkbk In Workbooks
If cbctrl.Caption = wkbk.Name Then
bExists = True
End If
Next wkbk
If bExists = False Then
Call RemoveButton(cbctrl.Caption, sCBName)
End If
Next cbctrl
End Sub
Sub AddButton(WBName As String, CBName As String)
Dim cbctrl As CommandBarControl
Dim MyBar As CommandBar
Dim cbcNew As CommandBarControl
Dim sCBName As String
Dim bExists As Boolean
Dim wkbk As Variant
If WBName <> "MockBar.xla" Then
Call AddBar(CBName) 'add the bar if it doesn't already exist
Set MyBar = CommandBars(CBName) 'set the bar to a variable
bExists = False
'check to see if a button exists for the wkbk
For Each cbctrl In MyBar.Controls
If cbctrl.Caption = WBName Then
bExists = True
End If
Next cbctrl
'if no button exists, add a button
If bExists = False Then
Set cbcNew = MyBar.Controls.Add(Type:=msoControlButton)
With cbcNew
.Style = msoButtonCaption
.Caption = WBName
.TooltipText = WBName
.OnAction = "'" & "modProcedures.ActivateWB """ & .Caption & """'"
End With
End If
End If
End Sub
Sub RemoveButton(WBName As String, CBName As String)
Dim cbar As CommandBar
Dim cbctrl As CommandBarControl
Dim MyBar As CommandBar
Dim bExists As Boolean
Dim wkbk As Variant
Dim iIndex As Long
Call AddBar(CBName) 'add the bar if it doesn't already exist
Set MyBar = CommandBars(CBName) 'set the bar to a variable
bExists = False
'check to see if a button exists for the wkbk & delete
For Each cbctrl In MyBar.Controls
If cbctrl.Caption = WBName Then
bExists = True
cbctrl.Delete
End If
Next cbctrl
End Sub