Jump to content

Passing parameters to Word VBA macros


Recommended Posts

All my searches in forum posts has not revealed how to pass parameters to macros in Word 2000 VBA. Here is a sample module in Word.

CODE
Public Sub ShowMe(Optional strText As String = "")

MsgBox "AutoIt says: " & strText

End Sub

And the companion AutoIt code:

$oWord = ObjGet("","Word.Application")
If @error = 1 or $oWord = 0 Then
    MsgBox(0,"","Failed to access Word")
Else
    $sMacro = "AutoItTest.ShowMe"
    $oWord.Application.Run($sMacro)
EndIf
oÝ÷ Ù8Z·
+ƬÀn¶Ü¢êìxÛh¥«,v¬¶¸§Èr§ëazX§zÚ-çèZ0x"µö¢Æ®¶­s`b33c·4Ö7&òÒgV÷C´WFôEFW7Bå6÷tÖRgV÷C²fײ6"3BfײgV÷C´VÆÆògV÷C²fײ6"3B

I've tried many variations, all with no result. Help!

Link to comment
Share on other sites

Here's the AutoIt COM syntax for .Run()

$oWord.Application.Run($sMacro, $Param1, $Param2, $Param3, ...)

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

The best documentation for the Word object model comes right out of Word's VBA helpfiles.

Open Word, hit ALT+F11 to get to VBA, then hit F2 to bring up the object browser, then type in your query. In your case, I looked up the word "Run" and looked through the list until I found Application -> Run, at which point I right clicked on it and selected "Help."

I guess the key to converting VBA to AutoIt is to remember that the biggest difference is in how you use variables (e.g. the dollar sign). For the most part, methods, properties, and collections work in AutoIt just like they do in VBA - meaning, once you understand the nuances you can work with just about any Office object practically as-is.

Of course, you may already know that VBA documentation and much more is available via MSDN, which will inevitably become very important to you as you continue working with the Office object models:

http://msdn2.microsoft.com/en-us/office/aa905497.aspx

Highly recommended if you're not already using it.

-S

Edited by Locodarwin
(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

Now I feel like a dork, because I did check VBA help when I was trying to figure this out and saw exactly what you're talking about. My mistake was that I thought the procedure name and parameters had to all be inside one parameter in AutoIt, as follows:

$sMacro = "Interact.AutoItDone, Success"
    $oWord.Application.Run($sMacro)

Of course we're all idiots at times. :) Live and learn. Thanks again.

Link to comment
Share on other sites

My mistake was that I thought the procedure name and parameters had to all be inside one parameter in AutoIt, as follows:

Believe me, I've made bolder mistakes than that while trying to get COM working with AutoIt. :)

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...