Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 52

Thread: Solved: Custom Floating Menu Bar

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Solved: Custom Floating Menu Bar

    How can I edit the code in this KB entry to make a floating menu bar, instead of one that sits on the default menu bar?

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=14

    ~Anne Troy

  2. #2
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    I did try:

    [vba]CommandBars("St&andards").Position = msoBarFloating[/vba]


    But it didn't change anything when I closed and reopened the doc.
    ~Anne Troy

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A workaround.

    Add the following at the end of the DocumentOpen sub
    [VBA]
    CommandBars.Add(Name:="MyMacros").Visible = True
    CommandBars("Menu Bar").Controls(11).Copy Bar:=CommandBars("MyMacros")
    [/VBA]

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    BTW, The signature macros would not run in 2000 until the extraneous spaces were deleted.

    eg
    from
    A(1) = Array("Suat's Signature " , " Ozgur " , 92)
    to
    A(1) = Array("Suat's Signature", "Ozgur", 92)

    Were the spaces deliberate, or created by the KB?

    MD

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    I don't know, but I'm not using those anyway...
    ~Anne Troy

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi DB,
    This should do the trick. I knew nothing about toolbar manipulation before, so well worth the effort! Replace the Document Open macro as follows:
    [VBA]
    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 i As Integer
    Dim A(12) As Variant
    Dim Check

    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("MyMacros").Position

    Set Mybar = CommandBars.Add(Name:="MyMacros", _
    Position:=msoBarFloating, Temporary:=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(11) = Array("Print Just This Page", "PrtPg", 159)
    A(12) = Array("Insert Landscape Page", "InsertLand", 6)


    Mybar.Visible = True
    Set Mytasks = Mybar.Controls.Add(Type:=msoControlPopup)
    Mytasks.Caption = "Te&mplates"

    'The ampersand (&) in the name of the menu underlines the letter that follows it to give
    'it a keyboard command (Alt-m) as many menus have.

    With CommandBars("MyMacros").Controls("Te&mplates").Controls
    For i = 1 To UBound(A)
    Set myButton = .Add(Type:=msoControlButton)
    With myButton
    .Caption = A(i)(0)
    .OnAction = A(i)(1)
    .FaceId = A(i)(2)
    End With
    Next i
    End With

    Else
    End If


    End Sub
    [/VBA]
    and change Document Close to
    [VBA]Private Sub Document_Close()

    'This closes the MyMacros 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("MyMacros").Delete
    ActiveDocument.AttachedTemplate.Saved = True

    End Sub
    [/VBA]

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Thank you, sweetie! Tho I prolly won't get to check it until late, late tonight or tomorrow.

    But I will letcha know!
    ~Anne Troy

  8. #8
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Quote Originally Posted by Dreamboat
    I prolly won't get to check it until late, late tonight or tomorrow.
    Unacceptable!! Check it now!!


  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    My email read:

    Here is the message that has just been posted:
    Unacceptable!! Check it now!! ;Whip

    I thought it was a comment on my coding!!!!

  10. #10
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I was going to post some code but when I came back MD had already done it, so all I have to say is that the code doesn't run with Option Explicit - it needs (at least) a declaration of myButton. Should we change it in the KB?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #11
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    No. Let's leave it wrong.

    ROFL!!

    Don't worry. I'll fix it tomorrow. I do recall having problems...
    ~Anne Troy

  12. #12
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Yeah, all variables should be declared and Option Explicit used in all code.

  13. #13
    VBAX Regular
    Joined
    Aug 2004
    Location
    On a 100 acre hobby farm in beautiful west Quebec.
    Posts
    87
    Location
    Very nice code mdmackillop! I have wondered if it would be possible to bring together bits and pieces of various toolbars I've cadged together from assorted projects over the years. Seeing this gives me hope.

    So how should the Mytasks and myButton variables be defined? I wasn't sure what data type to use (Dim myButton As ???). It does work without specifying a data type and I assume (by process of elimination mainly) that it would be either Variant or Object. Is there any advantage to using an explicit data type?
    Eric

    Experience is not what happens to a man; it is what a man does with what happens to him. ? Aldous Huxley

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Eric,
    All credit goes to Smozgur (see link in fist thread).
    Re the variable, I would go along with defining them, with the correct values if known, but I'm a bit unclear on some of these also. Trial and error!
    MD

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Eric,

    It's almost always better to use explicit data types.
    In this case myButton could be CommandBarControl or, better, CommandBarButton (the Type:=msoControlButton is a bit of a giveaway).

    If you don't know what type to use for an object, you can use the TypeName Function after it's been set, for example

    [VBA]Dim myObject
    Set myObject = CommandBars(1).Controls(1)
    Msgbox TypeName(myObject)[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Don't forget about the popup ...

    CommandBarPopup


  17. #17
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Okay. I'll be working on the menu this weekend. May need some other code help too. If anybody is going to be around and wants to make a few sheckles... it'd be nice to know who's willin'.
    ~Anne Troy

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I've added Option Explicit and split off the button naming to another sub to give some menu options on the toolbar.

  19. #19
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Well, if there's sheckels in it ....

    Seriously, I'll be around
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  20. #20
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Cool.
    ~Anne Troy

Posting Permissions

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