Running a .vbs file from a Word macro

L

Larry

How do I run a .vbs file from a Word macro? The below code opens the ..vbs file as a document. I just want to run the script in the .vbs file, not open the file.

Shell "C:\WINDOWS\NOTEPAD.EXE " & "C:\Documents\My system\test.vbs", 1
 
S

Steve Yandl

Larry,

The executable to use would be either cscript.exe or wscript.exe, not
notepad. You probably don't need the full path for Shell but both exe files
should be in your system32 folder. There are a number of arguments you can
use but the only one I've ever needed was "//B" that surpresses user
interface boxes that might pop up normally when the script runs. Cscript is
the console window version, wscript is the windows version.

Steve


How do I run a .vbs file from a Word macro? The below code opens the .vbs
file as a document. I just want to run the script in the .vbs file, not
open the file.

Shell "C:\WINDOWS\NOTEPAD.EXE " & "C:\Documents\My system\test.vbs", 1
 
L

Larry

Hi Steve,

I'm trying this

Shell "C:\Windows\wscript.exe " & "C:\Documents\My system\test.vbs", 1

And I get a message box saying, "There is no file extension in "C:\Documents\My"

Larry
 
J

Jay Freedman

You need to put in quotes, Chr$(34), around any path that contains
spaces:

Shell "wscript.exe " & Chr$(34) & "C:\Documents\My system\test.vbs" _
& Chr$(34), 1

The string passed to Shell is a DOS command line, which considers
spaces to be separators between multiple arguments. In your example,
it considers "C:\Documents\My" to be the (invalid) first argument, and
"system\test.vbs" to be a second (unused) argument.

Another point: wscript.exe is *not* in C:\Windows (unless you moved
it); as Steve said, it's in C:\Windows\system32. If you specify the
path as you did, the macro will say "file not found". Since the
system32 folder is in the Path environment variable by default,
wscript.exe will be found if you don't specify the path at all.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

Jean-Guy Marcil

Jay Freedman was telling us:
Jay Freedman nous racontait que :

Another point: wscript.exe is *not* in C:\Windows (unless you moved
it); as Steve said, it's in C:\Windows\system32. If you specify the

Actually, Steve did write that both wscript and cscript were in the system32
folder... ;-)

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

Larry

Thank you Jay, that works.

It's odd that the address needs the regular quotes (whose number is 34), AND the Chr$34 symbol for regular quotes.

Yes, wscript.exe is located in C:\Windows. I can't imagine my moving there it from another folder, but I suppose it's possible ...

Larry
 
T

Trevor L.

Larry said:
Thank you Jay, that works.

It's odd that the address needs the regular quotes (whose number is
34), AND the Chr$34 symbol for regular quotes.

Yes, wscript.exe is located in C:\Windows. I can't imagine my moving
there it from another folder, but I suppose it's possible ...

Larry

From a beginner in ASP/VBScript,
Is it possible to escape the quotes ?

For example, I have this JS code in one of my files:
code += " + ' onclick=\"LoadDiv(\\\'' + dayno + '\\\');return
false;\">'\n"
(It works, but I won't explain it)

Can you do the same or similar in VBScript ?
e.g. Shell "wscript.exe " \" "C:\Documents\My system\test.vbs" \", 1
 
G

Graham Mayor

You could use extra quotes if you prefer - """C:\Documents\My
system\test.vbs"""
The extra set is to prevent Word thinking that the second quote ends the
string.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Trevor L.

Graham said:
You could use extra quotes if you prefer - """C:\Documents\My
system\test.vbs"""
The extra set is to prevent Word thinking that the second quote ends
the string.

I am not the OP, but I am interested in the thread.

So that's two extra quotes, making 3 in all?
that is,
Shell "wscript.exe" """C:\Documents\My system\test.vbs""", 1
or I read in the thread somewhere that one could enclose it in brackets
 
J

Jay Freedman

The reason is that the regular quotes tell VBA where the parts of the string
start and end, while the Chr$(34) quotes become embedded inside the string
and tell the command processor where the file path starts and ends.

As Graham said in a parallel post, you can use two consecutive double-quote
characters instead of Chr$(34) to embed a quote mark inside a string, so
this syntax also works:

Shell "wscript.exe ""C:\Documents\My system\test.vbs"" ", 1

The first quote before wscript starts the string, and the last quote just
before the comma ends the string. The pair of double quotes before C: is
translated into one double quote in the command string, and the pair just
after .vbs is translated into another double quote. If you could view the
string just before the command processor received it, it would look like

wscript.exe "C:\Documents\My system\test.vbs"

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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