R
Rhino
I realize that this is slightly off-topic for this newsgroup but I've
already posted this question to microsoft.public.scripting.vbscript and
haven't received any replies after almost 24 hours.
Several days ago, Steve Yandl suggested using VBScript when I asked about a
way to launch Word invisibly and then have it execute a specific macro. He
included a sample VBScript that demonstrated the idea. I've adapted this
script to fit my specific situation and it works wonderfully.
I'm trying to add one small wrinkle to the script and can't figure out how
to do it. The macro that I am launching from the VBScript is now generating
a return code and I need my script to capture that return code in a
variable. At the moment, the return code is a boolean and will be true if
the macro/function works and false if it doesn't. However, I could change
the macro/function to return a long if that is easier in any way. By the
way, I'm assuming that VBA and VBScript follow the convention that False and
0 are synonymous, as are True and 1; please correct me if I'm wrong about
that!
Here's my script:
============================================================
' Get arguments into variables
If WScript.Arguments.Count > 0 Then
MsgBox "Too many arguments; expecting none."
WScript.Quit
End If
' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path
' Start Word Application, open resume.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\resume.doc")
'Run macro named createResumeFromFile, which has no arguments, and catch its
return code
dim bWaitOnReturn
bWaitOnReturn=True
retcde=oWd.run("createResumeFromFile",,bWaitOnReturn)
'oWd.Run "createResumeFromFile"
'Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing
Set objShell = Nothing
'Exit the script with the value of the return code from the macro/function
wscript.quit(retcde)
============================================================
When I execute this script, I get the following error on line (21, 1), which
is the 'retcde=oWd.run' line:
Microsoft VBScript runtime error: Wrong number of arguments or invalid
property assignment
Can anyone tell me what I'm doing wrong?
I thought that the problem might be that I didn't 'dim' retcde but adding a
'dim retcde' didn't help. I thought that the problem might be that 'dim
bWaitOnReturn' didn't define the variable as Boolean but adding 'as Boolean'
caused a syntax error. So I'm guessing that I've constructed the 'run'
statement incorrectly but don't know where to find documentation on the
correct formulation of the statement.
I really don't want to take a couple of days to develop a working knowledge
of VBScript at this point; I've already spent a few days getting a working
knowledge of VBA so that I could write my macros. I really need to get this
script working so that I can move on to other things.
Can anyone enlighten me on how to fix my script?
already posted this question to microsoft.public.scripting.vbscript and
haven't received any replies after almost 24 hours.
Several days ago, Steve Yandl suggested using VBScript when I asked about a
way to launch Word invisibly and then have it execute a specific macro. He
included a sample VBScript that demonstrated the idea. I've adapted this
script to fit my specific situation and it works wonderfully.
I'm trying to add one small wrinkle to the script and can't figure out how
to do it. The macro that I am launching from the VBScript is now generating
a return code and I need my script to capture that return code in a
variable. At the moment, the return code is a boolean and will be true if
the macro/function works and false if it doesn't. However, I could change
the macro/function to return a long if that is easier in any way. By the
way, I'm assuming that VBA and VBScript follow the convention that False and
0 are synonymous, as are True and 1; please correct me if I'm wrong about
that!
Here's my script:
============================================================
' Get arguments into variables
If WScript.Arguments.Count > 0 Then
MsgBox "Too many arguments; expecting none."
WScript.Quit
End If
' Find path for MyDocuments folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H5&)
strMyDocPath = objFolder.Self.Path
' Start Word Application, open resume.doc in MyDocuments
Set oWd = CreateObject("Word.Application")
oWd.Visible = False
Set oDoc = oWd.Documents.Open(strMyDocPath & "\resume.doc")
'Run macro named createResumeFromFile, which has no arguments, and catch its
return code
dim bWaitOnReturn
bWaitOnReturn=True
retcde=oWd.run("createResumeFromFile",,bWaitOnReturn)
'oWd.Run "createResumeFromFile"
'Save changes to doc on closing and quit Word
oDoc.Save
oDoc.Close
oWd.Quit
Set oWd = Nothing
Set objShell = Nothing
'Exit the script with the value of the return code from the macro/function
wscript.quit(retcde)
============================================================
When I execute this script, I get the following error on line (21, 1), which
is the 'retcde=oWd.run' line:
Microsoft VBScript runtime error: Wrong number of arguments or invalid
property assignment
Can anyone tell me what I'm doing wrong?
I thought that the problem might be that I didn't 'dim' retcde but adding a
'dim retcde' didn't help. I thought that the problem might be that 'dim
bWaitOnReturn' didn't define the variable as Boolean but adding 'as Boolean'
caused a syntax error. So I'm guessing that I've constructed the 'run'
statement incorrectly but don't know where to find documentation on the
correct formulation of the statement.
I really don't want to take a couple of days to develop a working knowledge
of VBScript at this point; I've already spent a few days getting a working
knowledge of VBA so that I could write my macros. I really need to get this
script working so that I can move on to other things.
Can anyone enlighten me on how to fix my script?