Results 1 to 10 of 10
  1. #1
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Who called me? #2

    (Word 2000)
    I've got a procedure that I want the user to be able to run from any of various toolbars (i.e. there will be an identical button for it on several different toolbars).

    In order for the procedure to proceed properly, it needs to know from which toolbar it is being run.

    Is there a way for me to have the procedure determine, at runtime, from which toolbar/control it's been summoned? (without requiring any further action by the user)

    I could implement a separate procedure for each toolbar (and hard-code in the toolbar's name), but it would be much better just to be able to implement the one.

    Any ideas or leads appreciated.

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Massachusetts
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Who called me? #2

    Gary,

    Don't have time to fully test this right now, but the CommandBars.ActionControl or CommandBars.ActiveMenuBar have some properties (name, tag, caption, etc.) that may do what you are looking for.

  3. #3
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Who called me? #2

    Gary,
    I think what you're after is:
    Commandbars.actioncontrol
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Who called me? #2

    Thanks James B. and Rory,

    That sounds promising and I'll try it out next chance I get to work on this project.

    In the meantime I thought of another possible approach, which would be to create a ControlsClickEvent class that could respond when the specific controls in question are clicked.

    That way, rather than having the procedure run as an "OnAction", it would run as a result of a ClickEvent (and Rory, this may get into the difference between the two, which was left hanging in an earlier thread):

    With the procedure running from OnAction, I can't pass it parameters:

    Public Sub TheProcedure()
    'statements

    But when set up to be called from a ControlClick event procedure, it can accept a parameter such as:

    Public Sub TheProcedure(strCmdBarName As String)
    'statements

    (well, I think that's how it can work, haven't tested it!)

    Anyway the method you've suggested sounds much easier so I'll definitely try that first!

    Thanks again,
    Gary

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Who called me? #2

    One fairly easy way to accomplish what you want to do is to just put all of the code in a general procedure. Then in the event procedures for each event you want to execute this code for just call the procedure with a parameter to tell it which event it is responding to.
    Legare Coleman

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Who called me? #2

    Hi again,

    Just following up with what worked and how it was gotten to work:

    CommandBars.ActionControl will return the control which triggered the procedure.
    Now, what I needed was the name of the CommandBar that the ActionControl was located on.
    If the ActionControl were directly on the "first level" of the CommandBar, then the following code will return the name of the CommandBar:

    Application.CommandBars.ActionControl.Parent.Name

    However, in my project the ActionControl is one level down, sitting under a drop-down menu (technically, I guess a "popup menu"). ".Parent.Parent.Name" wasn't a go.

    The alternative: assign each of the identical controls, on each of the different commandbars, a tag. Make the tag equivalent to the name of the commandbar on which is sits. Since you can't assign a tag to a control directly, code for assigning the tag was added to the procedure.
    The resulting code is a bit clunky, but works great:

    <pre>'set a tag for each relevant control; make the tag the same as the name of the commandbar
    Application.CommandBars("Custom1").Controls(" ").Controls("The Action Control").Tag = "Custom1"
    Application.CommandBars("Custom2").Controls(" ").Controls("The Action Control").Tag = "Custom2"
    'get the tag of the control which 'called' this procedure:
    strParentCbar = Application.CommandBars.ActionControl.Tag
    'now we know which commandbar we are working with
    Set cbrBar = CommandBars(strParentCbar)
    </pre>

    (Note: the "Controls(" ")" - one space between the quotes - references the drop-down menu which I've given a "name" equal to one space.)
    Thanks guys for putting me on the right track!

    Gary

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Who called me? #2

    Hi,

    The method you suggest is definitely what I'd have done, if I'd ended up using an event procedure.

    As it turned out, in this case it was possible to do the equivalent of getting and passing along the needed parameter without using event procedures (see my other post).

    Regards,
    Gary

  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Who called me? #2

    Gary,
    I think that commandbars.activemenubar will return the top-level menu bar if you've clicked on a control on a popup menu off a menu bar. Might save some coding unless you've already completed it.
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Who called me? #2

    Not only is it easy, it may be the best way as it avoids having to know the names of the controls/toolbars/menus.

  10. #10
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Who called me? #2

    Hi Rory,

    I did also try ActiveMenuBar (per James B.'s suggestion) but found that this returned the actual "Menu Bar" toolbar, rather than the custom toolbar my control lives on.
    (Maybe I'm missing some trick with using this....)

    Gary

Posting Permissions

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