Passing arguments through OnAction

How can I pass an argument to a procedure called from a Word97 CommandBar Button's OnAction property?
LVL 1
dhoverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

antratCommented:
not to sure what is you want to do but
this procedure will add a Custom menu button that when select will run the Macro called "MyMacro"


Sub PassOn()
Dim mybar As CommandBar, mycontrol As Object

Set mybar = Application.CommandBars("menu bar")
Set mycontrol = mybar.Controls _
    .Add(Type:=msoControlButton)
With mycontrol
    .FaceId = 25
    .OnAction = "MyMacro"
End With
mybar.Visible = True
End Sub

Sub MyMacro()
MsgBox "There are " & Application.CommandBars("Menu Bar").Controls.Count _
& " controls on the 'Menu Bar'."
End Sub

hope it helps

antrat

0
vboukharCommented:
I guess, the only way to do it - use Public variable and use procedure without arguements as a buffer procedure - something like my sample below:
Public arg As Integer '
' assume you have  Commandbar "Custom"
Sub testsub()
Set mybar = Application.CommandBars("Custom")
Set mycontrol = mybar.Controls _
    .Add(Type:=msoControlButton)
With mycontrol
    .FaceId = 2
    .OnAction = "MyMacro"
End With
mybar.Visible = True
End Sub

' buffer proc
Sub MyMacro()
  Select Case arg ' arg - Public variable, defined at Module level
     Case 1:
         Call MyMainProc(1, "first")
     Case 2:
         Call MyMainProc(2, "second")
     Case Else
         Call MyMainProc(3, "undefined")
  End Select
End Sub

' main proc
Sub MyMainProc(a As Integer, b As String)
  MsgBox "I've got number " & Str(a) & " and string " & b
  arg = arg + 1
End Sub

hope it helps.
0
mixaCommented:
You may use arguments in OnAction property, for example:

....
..OnAction = "'test 12'"
....

sub test(nn as variant)
   msgbox nn
end sub

Also there is Application.Caller property (at least in Excel VBA, and I think it may be in Word VBA). It stores information about object that is calling the macro. Your code could test command bar name there and use as condition in VBA code to select appropriate way.

<MIXA />
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mixaCommented:
note single quotes in the string literal above.
0
dhoverAuthor Commented:
MIXA,
Tried this, but get the error: "Method 'OnAction' of object 'CommandBarButton' Failed".

Here's my code:
            .OnAction = "'ShowHelp bmGETCELL'"

So I am using the inner single quotes...even tried all sorts of different configurations with the quotes...to no avail.

I'd rather use this type of method than the Application.Caller since this code is going in a Global template to implement a context sensitive-type help using Word documents as the help file. The argument will point to a bookmark in the document where the help text resides.
Using the Application.Caller would technically work, but as you could imagine, it would require a huge Select Case statement since we have about 1000 workgroup templates...any one of which could have a help file associated with it.
0
NoggyCommented:
So your Sub is: Sub ShowHelp( CellName as String) or something similar?
What I'm saying is that the argument you are passing is a string. If you intend it to relate to a constant (which is what it looks like), you'll have to actually use the constant's value in the OnAction statement instead of the Constant's variable name.
0
dhoverAuthor Commented:
OnAction = "'ShowHelp bmGETCELL'"

bmGETCELL is the _Name_ of the bookmark to lookup in the document. It is a variant string.

Are you saying that I need to use the actual _Index_ of the bookmark? This doesn't work either; I get the same error.
0
NoggyCommented:
No, if the argument is a string, then that's OK. It just looked like a Constant variable, that's all.........
0
dhoverAuthor Commented:
Ok, great. But any ideas why it's not working? It seems to work fine for you, right?

Here's a generalization of my receiving procedure:

Sub ShowHelp(bm as string)
    Const HlpPath As String = "C:\HelpDocs\"
    Dim HlpDoc As String
    Dim Temp As String
   
    Temp = ActiveDocument.AttachedTemplate
    HlpDoc = Mid(Temp, 1, InStr(1, Temp, ".")) & "doc"
   
    Documents.Open HlpPath & HlpDoc, , True
    Documents(HlpDoc).GoTo wdGoToBookmark, , , bm
End Sub
0
mixaCommented:
My mistake. Quotes doesn't work in Word VBA in OnAction property. Application.Caller is absent there as well :)

I think you could store value of argument somewhere in the document body (in the hidden area), or preferably in DocumentProperties (see File / Properties / Others ) In case if you're going to change argument at run-time you could use public variable instead of OnAction property.

Will it work ?

<MIXA />
0
antratCommented:
Hi dhover

To run the macro - ShowHelp(bm as string)
   
simply type as below:

OnAction = "ShowHelp"

antrat

0
dhoverAuthor Commented:
MIXA,

I don't think this will work for me.
0
mixaCommented:
Why not ?

This seems to be working:

Sub ShowHelp()
    Const HlpPath As String = "C:\HelpDocs\"
    Dim HlpDoc As String
    Dim Temp As String
     
    bm = ActiveDocument.CustomDocumentProperties("bm")
     
    Temp = ActiveDocument.AttachedTemplate
    HlpDoc = Mid(Temp, 1, InStr(1, Temp, ".")) & "doc"
     
    Documents.Open HlpPath & HlpDoc, , True
    Documents(HlpDoc).GoTo wdGoToBookmark, , , bm
End Sub

In this case each document may refer to separate bookmark. You may set "bm" value at run-time same as you were going to do with OnAction Argument.

Is anything wrong with it ?

Moreover, you can create set of bookmark in the document, and make "bm" depended on the current cursor position.

<MIXA />
0
dhoverAuthor Commented:
I'm sorry...I was having a hard visualizing this...still stuck on the OnAction.

I think this will work for the vast majority of our needs. There are a couple of templates though, where it won't work as easily since they have multiple hidden toolbars that come and go based on what the user is doing. Each of these have their own Help button that would point to the proper location (bookmark) in the help document.

I'll have to play with this a little more.

My other alternative is to use the Help Compiler which I've never used and I'm not quite sure that I want to.

Thanks,
Darryl
0
vboukharCommented:
Look at another approach - there is special property of CommandBarButton - Parameter. I guess it's your solution:
Sub AddOnAction()
  Dim MyBar As CommandBar, MyButt As CommandBarButton
  flag = False
  For Each MyBar In Application.CommandBars
    If StrComp(MyBar.Name, "Custom") = 0 Then
      flag = True
      Exit For
    End If
  Next
  If Not flag Then
    Set MyBar = Application.CommandBars.Add("Custom")
  Else
    Set MyBar = Application.CommandBars("Custom")
  End If
  flag = False
  For Each MyButt In Application.CommandBars("custom").Controls
    If StrComp(MyButt.Caption, "New") = 0 Then
      flag = True
      Exit For
    End If
  Next
  If Not flag Then
    Set MyButt = MyBar.Controls.Add(msoControlButton)
  Else
    Set MyButt = MyBar.Controls("New")
  End If
  MyButt.FaceId = 101
  MyButt.Caption = "New"
  MyButt.OnAction = "MySub"
  MyButt.Parameter = InputBox("What's parameter?", "Value", 0)
  MyBar.Visible = True
End Sub

Sub MySub()
  MsgBox Application.CommandBars("Custom").Controls("New").Parameter
End Sub

You can run AddOnAction - it asks you for parameter to be passed in your action sub. (and you can run it one more time to change this parameter)
I think it's a single way to pass parameters to OnAction procedure in MSWord.
I hope it helps.
Good luck!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhoverAuthor Commented:
First of all, thanks to all who responded! Your comments are appreciated.

The .Parameter trick works great combined with .ActionControl which substitutes for the Application.Caller function.

Here's the final code set...

....
..Parameter = "bmHelpTopicID"
....

Sub ShowHelp()
    Dim bm as String

    bm = CommandBars.ActionControl.Parameter
    Documents.Open "C:\HelpFile.doc"

    Selection.GoTo wdGoToBookmark,,,bm
End Sub
0
NoggyCommented:
vboukhar - Do you often visualise your posterior as a commandbutton :-) ?
0
vboukharCommented:
dhover,
thanks for points (and ActionControl - I don't use it before).
Noggy, it's casual concurrence - I use 100 (round number, isn't it?) for my code debugging and increased when added extra button to the same toolbar... But it's interesting thing for psycho-analyst :-) maybe somewhere in the depth of my mind I try to use my initials as much as possible?
VVVVVVVVVVVVVVVVVVVV :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.