开发者

how do we open a word file using vb script

开发者 https://www.devze.com 2023-01-18 15:27 出处:网络
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 sp开发者_如何学Pythonec

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 sp开发者_如何学Pythonecified", 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"


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"


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


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

Dim shell, quote, pgm, fname

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


How about this?:

set WshShell = Wscript.createObject("WScript.Shell")
WshShell.Run "Word"
WScript.Sleep 10

WshShell.AppActivate "Word"
0

精彩评论

暂无评论...
验证码 换一张
取 消