ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need syntax for RUNning a Word macro with an argument, called from an Excel macro (https://www.excelbanter.com/excel-programming/366462-need-syntax-running-word-macro-argument-called-excel-macro.html)

Steve[_84_]

Need syntax for RUNning a Word macro with an argument, called from an Excel macro
 


' IN EXECUTING A WORD MACRO FROM AN EXCEL MACRO USING AUTOMATION,
' WHAT IS THE SYNTAX FOR PASSING ARGUMENTS TO THE WORD MACRO?


'From EXCEL macro

Set oWord = GetObject(, "Word.application")
oWord.Visible = True
oWord.WindowState = wdWindowStateMinimize

' works when no argument present

oWord.Run MacroName:="MyWordMacro1"


' does NOT work with argument using this syntax

oWord.Run MacroName:="MyWordMacro1" arg1

-------------------------------------------------------------------------------------
WORD macros

Sub MyWordMacro1()
'
' macro code
'
end sub

Sub MyWordMacro2(arg1 As String)
'
' macro code
'
end sub


Jim Cone

Need syntax for RUNning a Word macro with an argument, called from an Excel macro
 
It doesn't appear as if Macro1 has any arguments?
Try this for Macro2, add a comma at the end of the called macro name.
If it still doesn't work then remove the argument name...
oWord.Run "MyWordMacro2", arg1

Curious:
does the Word application actually minimize?
why do you make the application visible and then minimize it?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Steve"
wrote in message

'From EXCEL macro
Set oWord = GetObject(, "Word.application")
oWord.Visible = True
oWord.WindowState = wdWindowStateMinimize

' works when no argument present

oWord.Run MacroName:="MyWordMacro1"


' does NOT work with argument using this syntax

oWord.Run MacroName:="MyWordMacro1" arg1

-------------------------------------------------------------------------------------
WORD macros

Sub MyWordMacro1()
'
' macro code
'
end sub

Sub MyWordMacro2(arg1 As String)
'
' macro code
'
end sub


Steve[_84_]

Need syntax for RUNning a Word macro with an argument, called from an Excel macro
 
Jim,
Thank you for your assistance. As you noted, there was a typo in my
original note in calling MyWordMacro1 with an argument. The intended
call was to MyMacro2 with the argument.

Your solution only works if ... MacroName:= ... is left out.

oWord.Run "MyWordMacro2", arg1

Yes, the Word application does miminize. The visible/minimize sequence
works for me because once the Word file is visiblle/mimimized I drive
through an Excel worksheet reading instructions on how to manipulate
the Word file (inserting hyperlinks, special styles for PDF bookmarks
to specified text strings, etc.) I need to pass an text string
argument to a Word macro so that it will search for and then apply the
bookmark-able style to the located string.

steve



Jim Cone wrote:
It doesn't appear as if Macro1 has any arguments?
Try this for Macro2, add a comma at the end of the called macro name.
If it still doesn't work then remove the argument name...
oWord.Run "MyWordMacro2", arg1

Curious:
does the Word application actually minimize?
why do you make the application visible and then minimize it?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Steve"
wrote in message

'From EXCEL macro
Set oWord = GetObject(, "Word.application")
oWord.Visible = True
oWord.WindowState = wdWindowStateMinimize

' works when no argument present

oWord.Run MacroName:="MyWordMacro1"


' does NOT work with argument using this syntax

oWord.Run MacroName:="MyWordMacro2" arg1

-------------------------------------------------------------------------------------
WORD macros

Sub MyWordMacro1()
'
' macro code
'
end sub

Sub MyWordMacro2(arg1 As String)
'
' macro code
'
end sub



Jim Cone

Need syntax for RUNning a Word macro with an argument, called from an Excel macro
 
Steve,
You are welcome.
Regards,
Jim Cone

"Steve"
wrote in message
Jim,
Thank you for your assistance...
-snip-


All times are GMT +1. The time now is 07:04 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com