我们如何使用vb脚本打开word文件

Bij*_*ose 3 vbscript ms-word office-interop

任何人都可以告诉我如何使用vbs windows脚本打开word文件.

我尝试了这两组vbs,但是当脚本"The system cannot find the file specified", errorcode: 80070002存在于指定位置时,会显示Windows脚本Host error().

我试过的第一个vbs:

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

第二个vbs我试过:

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"
Run Code Online (Sandbox Code Playgroud)

Hel*_*len 7

LittleBobbyTables在他的评论中解释了为什么你的第一个例子不起作用.

至于你的第二个例子,它不起作用,因为你没有在winword.exe路径和文件路径之间插入任何空格,所以你的命令行如下所示:

"C:\Program Files\Microsoft Office\Office\winword.exe""C:\UserGuide.doc"
Run Code Online (Sandbox Code Playgroud)


无论如何,像这样硬编码winword.exe路径是不可靠的,因为这个路径在64位和一些本地化Windows版本以及某些MS Office版本中是不同的.我建议您使用Word自动化对象:

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open "C:\UserGuide.doc"
Run Code Online (Sandbox Code Playgroud)