Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 27

Thread: Solved: Add to Standard Toolbar

  1. #1
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location

    Solved: Add to Standard Toolbar

    I've built new toolbars, but I can't quite figure out how to add a new Control(?) to Excel's "Standard" toolbar.

    On the Standard toolbar, I'd like to add a new menu, called "GSS", between the "Tools" and "Data" Controls.

    Then, I want to add various sub-Controls under my new "GSS" menu.

    Can someone, please, help me get started? How can I tell is the Standard toolbar is protected from changes? If it is protected, how do I unprotect it?

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi wagnet,

    Give the following a try, I've given you an example of everything you should need:[vba]Sub AddNewMenu()
    Dim MainBar As Object, NewMenu As Object
    On Error Resume Next
    Set MainBar = Application.CommandBars("Worksheet Menu Bar")
    Set NewMenu = MainBar.Controls.Add(type:=10, _
    Before:=MainBar.Controls("Data").Index) '10=msoControlPopup

    NewMenu.Caption = "&GSS"
    With NewMenu.Controls.Add
    .Caption = "&First menu option" 'the & makes it alt-key friendly
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "First menu option"
    End With
    With NewMenu.Controls.Add
    .Caption = "&Second menu option"
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "Second menu option"
    End With
    With NewMenu.Controls.Add
    .Caption = "&Third menu option"
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "You get the idea"
    End With
    End Sub
    Sub RemoveNewMenu()
    Dim cBc As Object
    On Error Resume Next
    For Each cBc In Application.CommandBars("Worksheet Menu Bar").Controls
    If cBc.Caption = "&GSS" Then
    cBc.Delete
    Exit For
    End If
    Next 'cBc
    End Sub[/vba]Matt

  3. #3
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location
    Thanks, Matt. You make it look so easy.

    How do I check to see if GSS already exists?

    Basicly, I want to create the GSS Menu, then continue to add sub-menu items (Controls) under it, one at a time, as I add additonal macro/functionality.

    I assume deleting the sub-menu (Controls) is the same as the RemoveNewMenu() macro --- just identify the Control name that I want to remove.

  4. #4
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location
    ooops.

  5. #5
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    I think a good practice is to build and delete the menu each time excel is started. in ThisWorkbook, try adding the following:

    [vba]
    Sub Workbook_AddinInstall()
    AddNewMenu
    end sub

    Sub Workbook_AddinUninstall()
    KillMenu

    Dim xlaName As String
    Dim i As Long

    With ThisWorkbook
    For i = 1 To AddIns.Count
    xlaName = AddIns(i).Name

    If xlaName = "YOURFILE.xla" Then 'be sure to put your filename here
    AddIns(i).Installed = False
    End If

    Next i
    End With
    End Sub

    Sub Workbook_BeforeClose() 'NOTE: I have a bunch of other stuff I do
    RemoveNewMenu 'here as well; not 100% certain this is all you need
    end sub

    Sub Workbook_Open()
    AddNewMenu
    end sub
    [/vba]
    I usually find that other folks here have much more efficient ways of doing things than I do, so please dont consider this to be "the" solution. It's a weekend, so I thought I would throw my 2 cents in since i had a few moments.

    This should address your question about checking if the menu already exists; since it is built each time Excel is started (or the add in is installed) and removed upon exit/uninstall, you shouldnt have to worry about multiple menus being built (I HOPE!)

    regarding adding additional items as you add functionality, just add more items using the code mvidas provided above. if you want to remove something, comment out the "with...end with" part or delete it. You should not have to use the RemoveNewMenu routine to remove items from your new menu; that is used to remove the menu from the toolbar itself.

    hope this makes sense!

  6. #6
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location
    Thanks for the response (and the sample code).

    The "Standard" toolbar has several Controls (e.g. Format). Some of the sub-Control entries are grouped and a line divides the groupings (e.g. the line after the Format\Sheet Control.

    How is that line created - or those groupings created?

    Is it possible to change the font, text color, or background color or a specific sub-Control (is this referred to as a child?)?

    I see how an Icon can associate the Control with the .FaceId command.

    How do I assign an Alt-Key to a specific Control?

  7. #7
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    .BeginGroup = True puts a line above the entry. Regarding your other questions:

    changing font - I dont think so, but I am not sure

    for the alt keys, you can use the "&" in the caption to define which letter will be underlined and work as a shortcut once the menu is selected/expanded, as "G" is in the example above.

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    As a side note, if you are making use of a Class module with events for your command buttons, you'll want to use the Workbook_Open method as well as the Install/Uninstall.

  9. #9
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Does anybody know how to make one of the menu options branch off to the right with another drop down and more options? For instance if you go to Data then down to Filter it has an arrow pointing to the right with another drop down with more options. Need help if you got it.

  10. #10
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    gsouza,

    Usually, opening another question for this would be preferred. But since it is related, I can answer it here.

    Using my example above, instead of doing:[vba] With NewMenu.Controls.Add
    .Caption = "&First menu option" 'the & makes it alt-key friendly
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "First menu option"
    End With[/vba]Instead of adding a normal control, add another with Type:=10 (the menu, like we did to add it to the worksheet menu bar):[vba] With NewMenu.Controls.Add(type:=10, Before:=1)
    .Caption = "new menu"
    With .Controls.Add
    .Caption = "&First menu option" 'the & makes it alt-key friendly
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "First menu option"
    End With
    End With[/vba]Matt

  11. #11
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    I thought this was the place to reply since I was working on this entry. Thanks for the help, it works great. It impressed my boss

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    That is a PopUp and not a button. Instead of using ID numbers, I like to use the text constant..

    msoControlPopup
    msoControlButton
    etc

  13. #13
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Quote Originally Posted by firefytr
    That is a PopUp and not a button. Instead of using ID numbers, I like to use the text constant..

    msoControlPopup
    msoControlButton
    etc
    You never know if someone forgets to set their Office reference

  14. #14
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Inconceivable!! LOL!

  15. #15
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Is it possible to place an Icon before the text?

  16. #16
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    There sure is.. in the With block when adding a new control, specify the .FaceId property to set that icon:[vba] With NewMenu.Controls.Add
    .FaceId = 4
    .Caption = "&First menu option" 'the & makes it alt-key friendly
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "First menu option"
    End With
    With NewMenu.Controls.Add(type:=10, Before:=1)
    .Caption = "new menu"
    With .Controls.Add
    .FaceId = 5
    .Caption = "&First menu option" 'the & makes it alt-key friendly
    .OnAction = "'" & ThisWorkbook.Name & "'!ModuleName.MacroName"
    .Tag = "First menu option"
    End With
    End With[/vba]You can get a listing of all the face id's using an addin like http://www.dicks-blog.com/archives/2...-viewer-addin/
    Matt

  17. #17
    VBAX Contributor
    Joined
    Dec 2004
    Posts
    122
    Location
    Cool beans, Thank you for all your help. Anything off your head that I can do with this that is really neat? Any Suggestions that you might want to share?

  18. #18
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location

    Visible = True/False - but what about greyed out?

    Toolbars can be made "visible" (myBar.Visible = True), but can an individual Control or Item (.AddItem "GSS Tool") be set to .Visable = False?

    I noticed in the Standard Excel toolbar, the Paste Control is low-lighted (e.g. greyed out) when nothing is on the clipboard. How is this accomplished?

  19. #19
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by wagnet
    ...
    I noticed in the Standard Excel toolbar, the Paste Control is low-lighted (e.g. greyed out) when nothing is on the clipboard. How is this accomplished?
    Insert your numbers for M and N...
    [VBA]
    Application.CommandBars(M).Controls(N).Enabled = False
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  20. #20
    VBAX Regular
    Joined
    Jan 2006
    Posts
    20
    Location
    How do I determine the Index number for the CommandBars and/or Controls?

Posting Permissions

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