I've a code to add a button to a bar every time a file is opened. However for some reason it keeps adding the same button to the same bar if I open other or new files within the same session. I thought the if my button is nothing would handle this issue but unfortunately it doesn't. Can anyone help?
[vba]Sub AddCustomBar()
Dim MyBar As CommandBar
Dim MyButton As CommandBarButton
On Error Resume Next
Set MyBar = CommandBars("Custom Bar")
If MyBar Is Nothing Then
Set MyBar = CommandBars.Add(Name:="Custom Bar", Position:=msoBarTop, Temporary:=True)
MyBar.Visible = True
End If
Set MyButton = MyBar.Controls("MyButton")
If MyButton Is Nothing Then
Set MyButton = MyBar.Controls.Add(Type:=msoControlButton)
With MyButton
.Style = msoButtonCaption
.Caption = "Linked Tasks"
.OnAction = "LinkedTasks"
End With
End If
End Sub[/vba]