Consulting

Results 1 to 4 of 4

Thread: Solved: Dynamic Button Bar

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location

    Solved: Dynamic Button Bar

    Ok... So I get an idea in my head and I am on . I start reading various post and KB's in VBA express and the idea evolves.

    Finally, I find my self with an interesting idea BUT...

    I used the KB to build a Custom Menu. However, I don't want it in the menu bar, but would rather have it as a ToolBar. So, I to the VBA Guru's and hope someone will help me see what I am missing here.

    I imagine that I am looking for a CommandBar Type but am having absolutely no luck finding it.

    [vba]
    Sub Custom_Toolbar()
    Dim Mybar As CommandBar
    Dim cmd As CommandBarPopup
    Dim iInt As Integer
    Dim A(2) As Variant

    CustomizationContext = ActiveDocument.AttachedTemplate

    On Error Resume Next
    'This checks if the menu already exists. If it does, it does not create a new one.

    CommandBars("Menu Bar").Controls("Te&mplates").Caption = "Te&mplates"
    If Not Err.Number = 0 Then
    'Note that the parts of the array are ("Title of menu option", "Macro to Run", FaceID for toolbar button)
    A(1) = Array("New Visit ", "NewTemplate", 18)
    A(2) = Array("Save Visit ", "SaveTemplate", 3)
    With CommandBars("Menu Bar").Controls
    .Add(Type:=msoControlPopup, Before:=11).Caption = "Te&mplates"
    End With
    For iInt = 1 To UBound(A)
    With CommandBars("Menu Bar").Controls("Te&mplates").Controls
    Set myButton = .Add(Type:=msoControlButton)
    With myButton
    .Caption = A(iInt)(0)
    .OnAction = A(iInt)(1)
    .FaceId = A(iInt)(2)
    End With
    End With
    Next iInt
    End If
    End Sub
    [/vba]

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hello Gman,
    Here is an example using an array for you to play with. Put it in the ThisDocument module:
    [vba]
    Option Explicit
    Private Sub Document_Open()
    'The Dim statements make the rest of the code easier to create.

    Dim Mybar As CommandBar
    Dim cmd As CommandBarPopup
    Dim MyTasks As CommandBarPopup
    Dim MyButton As CommandBarButton
    Dim i As Integer
    Dim A(12) As Variant
    Dim Check As Long

    CustomizationContext = ActiveDocument.AttachedTemplate

    On Error Resume Next
    'This checks if the menu already exists. If it does, it does not create a new one.

    Check = CommandBars("My Macros").Position

    Set Mybar = CommandBars.Add(Name:="My Macros", _
    Position:=msoBarFloating, Temporary:=True)
    Mybar.Visible = True

    If Not Err.Number = 0 Then

    'Note that the parts of the array are
    '( " Title of menu option " , " Macro to Run " , FaceID for toolbar button)

    A(1) = Array("Suat's Signature", "Ozgur", 92)
    A(2) = Array("Anne's Signature", "Smith", 85)
    A(3) = Array("Nancy's Signature", "Johnson", 89)
    A(4) = Array("Dreamboat's Signature", "Dreamboat", 80)
    A(5) = Array("Mickey's Signature", "Mouse", 98)
    A(6) = Array("Insert Photo", "InsPic", 280)
    A(7) = Array("Fix Picture", "FixPix", 1363)
    A(8) = Array("Add Photo Heading", "PhotoCont", 314)
    A(9) = Array("Insert Stopping Point", "StopPoint", 2528)
    A(10) = Array("Find Last Stopping Point", "StartHere", 2526)
    A(12) = Array("Print Just This Page", "PrtPg", 159)
    A(11) = Array("Insert Landscape Page", "InsertLand", 6)

    'Set button menus
    'SetMenus Toobar name, Button caption, Array to Start, Array to end, Array Name

    'Add signatures
    SetMenus "My Macros", "Si&gnatures", 1, 5, A
    'Add other macros
    SetMenus "My Macros", "&Macros", 6, 11, A
    'Add Print Macros
    SetMenus "My Macros", "&PrintOptions", 12, 12, A

    Else
    End If
    End Sub

    Private Sub SetMenus(BarName, Cap, Start, Endd, MacroList)
    Dim MyTasks As CommandBarPopup
    Dim MyButton As CommandBarButton
    Dim i As Integer
    Dim Mybar As CommandBar

    Set Mybar = CommandBars(BarName)
    Set MyTasks = Mybar.Controls.Add(Type:=msoControlPopup)
    MyTasks.Caption = Cap
    With CommandBars("My Macros").Controls(Cap).Controls
    For i = Start To Endd
    Set MyButton = .Add(Type:=msoControlButton)
    With MyButton
    .Caption = MacroList(i)(0)
    .OnAction = MacroList(i)(1)
    .FaceId = MacroList(i)(2)
    End With
    Next i
    End With
    End Sub



    Private Sub Document_Close()

    'This closes the My Macros toolbar when the document is closed. It also keeps the user from
    'changing the template. This is what we call an *on-event* procedure (macro) because it is
    'run when the document is closed.

    On Error Resume Next
    CommandBars("My Macros").Delete
    ActiveDocument.AttachedTemplate.Saved = True

    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Nov 2005
    Posts
    39
    Location
    Lucas,

    Thanks, this was exactly what I was looking for. Just a little tweaking here and there and it works like a charm.

    Thanks!!

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Glad it helped.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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