Кажется вероятным, что во время отладки вы добавили какой-то экземпляр панели инструментов AddIn, и теперь он существует в этом состоянии.Таким образом, вы должны убедиться, что вы всегда удаляете его , прежде чем пытаться добавить его.
При некоторых других незначительных ре-факторингах я бы порекомендовал так:
Option Explicit
' Give the toolbar a name
Const MyToolbar As String = "Helpful Stuff"
Dim oToolbar As CommandBar
Sub Auto_Open()
Dim oButton As CommandBarButton
Call AddMe
On Error GoTo ErrorHandler
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
' And set some of the button's properties
With oButton
.DescriptionText = "This is my first button" 'Tooltip text when mouse if placed over button
.Caption = "Do Button1 Stuff" 'Text if Text in Icon is chosen
.OnAction = "Button1" 'Runs the Sub Button1() code when clicked
.Style = msoButtonIcon ' Button displays as icon, not text or both
.FaceId = 52 ' chooses icon #52 from the available Office icons
End With
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
' Repeat the above for as many more buttons as you need to add
' Be sure to change the .OnAction property at least for each new button
With oButton
.DescriptionText = "This is my second button" 'Tooltip text when mouse if placed over button
.Caption = "Do Button2 Stuff" 'Text if Text in Icon is chosen
.OnAction = "Button2" 'Runs the Sub Button2() code when clicked
.Style = msoButtonIcon ' Button displays as icon, not text or both
.FaceId = 51 ' chooses icon #51 from the available Office icons
End With
' Now add a button to the new toolbar
Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)
With oButton
.DescriptionText = "This is my third button" 'Tooltip text when mouse if placed over button
.Caption = "Do Button3 Stuff" 'Text if Text in Icon is chosen
.OnAction = "Button3" 'Runs the Sub Button3() code when clicked
.Style = msoButtonIcon ' Button displays as icon, not text or both
.FaceId = 50 ' chooses icon #50 from the available Office icons
End With
NormalExit:
Exit Sub ' so it doesn't go on to run the errorhandler code
ErrorHandler:
'Just in case there is an error
MsgBox Err.Number & vbCrLf & Err.Description
Resume NormalExit:
End Sub
Вам необходимо добавить следующие две процедуры:
Private Sub RemoveMe()
' Removes the toobar if it already exists:
On Error Resume Next
CommandBars(MyToolbar).Delete
End Sub
Private Sub AddMe()
' If the toolbar already exists, remove it
Call RemoveMe
Set oToolbar = CommandBars.Add(Name:=MyToolbar, _
Position:=msoBarFloating, Temporary:=True)
' You can set the toolbar position and visibility here if you like
' By default, it'll be visible when created. Position will be ignored in PPT 2007 and later
oToolbar.Top = 150
oToolbar.Left = 150
oToolbar.Visible = True
End Sub