Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Toolbar menu item calls macro with argument

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter - 18 Dec 2007 21:49 GMT
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
Tony Jollans - 18 Dec 2007 23:49 GMT
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

Signature

Enjoy,
Tony

>I know how to create a custom toolbar with menu items that invoke macros.
>The
[quoted text clipped - 13 lines]
> Thanks,
> Peter
Jay Freedman - 19 Dec 2007 00:33 GMT
>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

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Peter - 19 Dec 2007 12:56 GMT
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

> >You can't pass a parameter but you can tell which menu item invoked the
> >macro by querying ...
[quoted text clipped - 50 lines]
> Microsoft Word MVP        FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.