views:

40

answers:

3

could anyone plz tell me how to open word files using vbs windows scripting.

I tried out these two set of vbs, but windows script Host error ("The system cannot find the file specified", errorcode: 80070002) is getting displayed eventhough the file exists at the specified location.

the first vbs i tried out:

Dim sAppPath
Dim sPrgFolder
sPrgFolder=CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%") 
sAppPath =sPrgFolder + "c:\UserGuide.doc"
WScript.CreateObject("WScript.Shell").Run sAppPath)

second vbs i tried:

OPTION EXPLICIT
dim fso, ws, file_to_open, OFFICE_PATH
Set ws = WScript.CreateObject("WScript.Shell")
OFFICE_PATH = "C:\Program Files\Microsoft Office\Office"
file_to_open = CHR(34) & "C:\UserGuide.doc" & CHR(34)
ws.Run CHR(34)& OFFICE_PATH & "\winword.exe" & CHR(34) & file_to_open, 0, "FALSE"
+1  A: 
OPTION EXPLICIT
dim fso, ws, file_to_open, OFFICE_PATH
Set ws = WScript.CreateObject("WScript.Shell")
OFFICE_PATH = "C:\Program Files\Microsoft Office\Office"
file_to_open = CHR(34) & "C:\UserGuide.doc" & CHR(34)
ws.Run CHR(34) & OFFICE_PATH & "\winword.exe " & CHR(34) & file_to_open, 0, "FALSE"

try this revised code, check the modifications in last line :)

regexhacks
A: 

Thanks buddies..... i got it working with these vbs.

Dim shell Dim quote Dim pgm Dim fname

set shell = WScript.CreateObject("WScript.Shell") quote = Chr(34) pgm = "WINWORD" fname = "C:\UserGuide.doc" shell.Run quote & pgm & quote & " " &fname

bjoy
+2  A: 

LittleBobbyTables explained in his comment why your first example doesn't work.

As for your second example, it doesn't work because you don't insert any spaces between the winword.exe path and the file path, so your command line looks like this:

"C:\Program Files\Microsoft Office\Office\winword.exe""C:\UserGuide.doc"


Anyway, hard-coding the winword.exe path like this is unreliable, as this path is different in 64-bit and some localized Windows versions as well as for some MS Office versions. I suggest that you use Word automation objects instead:

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open "C:\UserGuide.doc"
Helen