Toolbar menu item calls macro with argument

P

Peter

I know how to create a custom toolbar with menu items that invoke macros. The
question is, is it possible to create sucha a menu item that calls a macro
that takes an argument. I have a macro that takes an argument that then
processes the argument using a Select Case... construct. I'd like to create
one menu item for each Case. Each menu item would invoke the same macro but
with a different argument. I hate to have to create individual macros for
each menu item that in turn call the main macro with the argument passed to
it. I suppose this is a workaround but if there is a way to do it the other
way, it would be a lot more efficient.

Thanks,
Peter
 
T

Tony Jollans

You can't pass a parameter but you can tell which menu item invoked the
macro by querying ...

CommandBars.ActionControl

... which seems like it will fit the bill
 
J

Jay Freedman

You can't pass a parameter but you can tell which menu item invoked the
macro by querying ...

CommandBars.ActionControl

... which seems like it will fit the bill

As a concrete example, run the first of these macros to create a
custom command bar with two buttons, both of which invoke the same
second macro. The second macro displays the .Tag of the clicked
button; you could use it, for example, to save the document with a
different name or to a different path. Or you could use the .Tag
string to simulate an enumerated type, and use the value in a Switch
Case statement to control the action.

Each button also has a .Parameter property that can serve as a second
"argument" if needed.

Sub SetupCB()
' run me once
Dim myCB As CommandBar
Dim myButn1 As CommandBarControl, myButn2 As CommandBarControl

Application.CustomizationContext = ActiveDocument
Set myCB = CommandBars.Add(Name:="CustomA",
Position:=msoBarFloating)
Set myButn1 = myCB.Controls.Add(Type:=msoControlButton)
Set myButn2 = myCB.Controls.Add(Type:=msoControlButton)
myCB.Enabled = True
myCB.Visible = True
With myButn1
.Style = msoButtonCaption
.Caption = "1"
.Tag = "first.doc"
.OnAction = "DifferentActions"
End With
With myButn2
.Style = msoButtonCaption
.Caption = "2"
.Tag = "second.doc"
.OnAction = "DifferentActions"
End With
End Sub

Sub DifferentActions()
' called by the buttons on the custom bar
MsgBox CommandBars.ActionControl.Tag
End Sub
 
P

Peter

Thank you, that was very helpful. It turns out that my select statement uses
an enumerated type and all I had to do was to set that in the .Tag field and
it worked.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top