Consulting

Results 1 to 2 of 2

Thread: Solved: Add button to bar

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Posts
    30
    Location

    Solved: Add button to bar

    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]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [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("Linked Tasks")

    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]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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