Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do we open a word file using vb script

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"
like image 919
Bijoy K Jose Avatar asked Jan 22 '23 08:01

Bijoy K Jose


2 Answers

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"
like image 58
Helen Avatar answered Feb 19 '23 05:02

Helen


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 :)

like image 31
Rajeev Avatar answered Feb 19 '23 05:02

Rajeev