This is straying from the original problem, but I been thinking about this (Ohhh trouble - John's been 'thinking')...
Some will no doubt disagree, but personally, I think the small added procedure below would be a far more sensible procedure than the one given in the MS KB example above...
E.G. A user may have one of these command bars that they have taken the trouble to create many buttons for and then re-named them & assigned links to all of them - but the command bar may be hidden and the user may have forgotten they've hidden it and thinks "Damn! I'll have to start all over..."
So, instead of blindly deleting & creating a command bar that may already exist but is hidden from view, wouldn't this be more logical? - if they dont already have one THEN, and only then, make one - otherwise, just unhide it. (Example below)
If for any reason they really want to delete their modified one and make a new one, they can do that manually by going to view/toolbars/delete... (a message with this advice could also be added)
' If it already exists. This procedure makes the toolbar visible.
Sub AddNewCB()
Dim CBar As CommandBar, Count As Integer
Count = 0
For Each CBar In CommandBars
If CBar.Name = "Sample Toolbar" Then Count = Count + 1
Next CBar
If Count = 0 Then MakeNewCB
If Count > 1 Then MsgBox ("ERROR! There are " & Count & " toolbars - Delete " & Count - 1 & " !") Else
On Error Resume Next
CommandBars("Sample Toolbar").Visible = True
End Sub
This procedure creates a new toolbar.
Function MakeNewCB() '
Dim CBar As CommandBar, CBarCtl As CommandBarControl
' Create a new floating toolbar and make it visible.
On Error Resume Next
Set CBar = CommandBars.Add(Name:="Sample Toolbar", Position:= _
msoBarFloating)
CBar.Visible = True
' Create a button with text on the bar and set some properties.
Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton)
With CBarCtl
.Caption = "Button"
.Style = msoButtonCaption
.TooltipText = "Display Message Box"
.OnAction = "Message"
End With
' Create a button with an image on the bar and set some
' properties.
Set CBarCtl = CBar.Controls.Add(Type:=msoControlButton)
With CBarCtl
.FaceId = 1000
.Caption = "Toggle Button"
.TooltipText = "Toggle First Button"
.OnAction = "ToggleButton"
End With
' Create a combo box control on the bar and set some properties.
Set CBarCtl = CBar.Controls.Add(msoControlComboBox)
With CBarCtl
.Caption = "Drop Down"
.Width = 100
.AddItem "Create Button", 1
.AddItem "Remove Button", 2
.DropDownWidth = 100
.OnAction = "AddRemoveButton"
End With
End Function
This procedure is called from a button on the toolbar.
It toggles the Visible property of another button on the bar.
Function ToggleButton()
Dim CBButton As CommandBarControl
On Error GoTo ToggleButton_Err
Set CBButton = CommandBars("Sample Toolbar").Controls(1)
CBButton.Visible = Not CBButton.Visible
Exit Function
ToggleButton_Err:
MsgBox "Error " & Err.Number & vbCr & Err.Description
Exit Function
End Function
This procedure is called from a combo box on the toolbar
It adds a button to the bar or removes it
Function AddRemoveButton()
Dim CBar As CommandBar, CBCombo As CommandBarComboBox
Dim CBNewButton As CommandBarButton
On Error GoTo AddRemoveButton_Err
Set CBar = CommandBars("Sample Toolbar")
Set CBCombo = CBar.Controls(3)
Select Case CBCombo.ListIndex
'If Create Button is selected, create a button on the bar
Case 1
Set CBNewButton = CBar.Controls.Add(Type:=msoControlButton)
With CBNewButton
.Caption = "New Button"
.Style = msoButtonCaption
.BeginGroup = True
.Tag = "New Button"
.OnAction = MsgBox("There is a new button!")
End With
' Find and remove the new button if it exists.
Case 2
Set CBNewButton = CBar.FindControl(Tag:="New Button")
CBNewButton.Delete
MsgBox ("Button deleted!")
End Select
Exit Function
AddRemoveButton_Err:
' If the button does not exist.
If Err.Number = 91 Then
MsgBox "Cannot remove button that does not exist!"
Exit Function
Else
MsgBox "Error " & Err.Number & vbCr & Err.Description
Exit Function
End If
End Function
Function Message()
MsgBox "You pressed a toolbar button!"
End Function