Ping Steve Yandl or other VBScript gurus

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?
 
S

Steve Yandl

Rhino,

I don't have time to do testing this morning but on quick glance I think you
should change your line
bWaitOnReturn = True
to
Const vbTrue = -1
then use vbTrue as the argument. You may also have to drop the parenthesis
around the arguments to oWd.Run launching the macro but I'm not certain
without testing.

In vbScript, all variables are variants. It's still not a bad idea to use
Dim statements but the variables are not 'typed'.

I've not seen your VBA code but generally you're better of going entirely
with vbs or entirely with VBA. Use of vbs to run a macro in Word can be
handy if you need to feed a command line argument or a couple other
situations but more often than not I tend to call the scripting components I
require from within my VBA routines rather than the other way around. I
suspect it would be simple enough to duplicate what your VBA routine does
and convert entirely to VBS which is another option where you could avoid
messing with the transfer of output between routines.


Steve
 
J

Jean-Guy Marcil

Rhino was telling us:
Rhino nous racontait que :
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?

It seems to me that you have one comma too many:
retcde=oWd.run("createResumeFromFile",,bWaitOnReturn)
should be
retcde=oWd.run("createResumeFromFile",bWaitOnReturn)
No?


Show us the "createResumeFromFile" Sub declaration line.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Rhino

Thanks for your quick reply, Steve, and sorry to bug you when you are
obviously busy.

The good news is that I've solved the problem. I finally got a reply to my
post on the vbscript newsgroup and that got me going in the right direction.
Here's my final script, for the sake of anyone following this thread, now or
in the future via the archive:

===============================================================
' 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
retcde=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.
'That will be zero from a successful execution or 1 otherwise.
wscript.quit(retcde)
===============================================================

My basic mistake was imitating something I found in a Google search on the
problem; that post suggested that the Run command needed a boolean passed to
ensure that the Run command waited for a return code (or at least, that's my
guess as to what the boolean was supposed to do). As soon as I removed the
boolean from the Run command, everything worked fine and the return code was
properly obtained.

For what it's worth, the script failed when I tried your 'Const vbTrue = -1'
suggestion; the message said something about redefining something that
already existed. I'm guessing that vbTrue is already implicitly defined as a
constant somewhere in VBScript and it didn't like me messing with that
definition. As I said, I didn't need that parameter in the Run anyway; that
was just a wild goose chase thanks to the newsgroup post I saw in Google. Oh
well, that's life :)

By the way, I appreciate your point about not mixing VBA and VBScript; that
makes a lot of sense to me. Unfortunately, I don't have the time to learn
how to do what I want to do entirely in VBA or VBScript; I've already spend
way more time on VBA than I'd planned and am way behind on other things. So,
now that everything is working fine, I'm just going to leave things the way
they are and worry about doing a cleaner job on my next VB-related project,
when and if that ever happens.

Thanks again for your help, Steve!


Steve Yandl said:
Rhino,

I don't have time to do testing this morning but on quick glance I think
you should change your line
bWaitOnReturn = True
to
Const vbTrue = -1
then use vbTrue as the argument. You may also have to drop the
parenthesis around the arguments to oWd.Run launching the macro but I'm
not certain without testing.

In vbScript, all variables are variants. It's still not a bad idea to use
Dim statements but the variables are not 'typed'.

I've not seen your VBA code but generally you're better of going entirely
with vbs or entirely with VBA. Use of vbs to run a macro in Word can be
handy if you need to feed a command line argument or a couple other
situations but more often than not I tend to call the scripting components
I require from within my VBA routines rather than the other way around. I
suspect it would be simple enough to duplicate what your VBA routine does
and convert entirely to VBS which is another option where you could avoid
messing with the transfer of output between routines.


Steve
 
R

Rhino

Jean-Guy Marcil said:
Rhino was telling us:
Rhino nous racontait que :


It seems to me that you have one comma too many:
retcde=oWd.run("createResumeFromFile",,bWaitOnReturn)
should be
retcde=oWd.run("createResumeFromFile",bWaitOnReturn)
No?
Basically, my statement was inspired by a newsgroup post I found in Google
which was attempting to solve the same problem; the proposed solution in
that post showed two commas between the two parameters so that's what I did
in my statement. But I don't have a VBScript reference manual so I was
working blind: I couldn't check that syntax to be sure it was valid - and I
still can't unless someone can point me to a VBScript reference.
Show us the "createResumeFromFile" Sub declaration line.
That won't be necessary now; I've solved the problem. See my reply to Steve
Yandl elsewhere in this thread. Apparently, I didn't need any parameters for
the Run command beyond the macro name: that's what was causing my errors.

Et vous aussi! Merci pour votre aide!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top