【发布时间】:2022-05-22 21:14:23
【问题描述】:
谁能告诉我如何使用 vbs windows 脚本打开 word 文件。
我尝试了这两组vbs,但是即使文件存在于指定位置,也会显示windows script Host error ("The system cannot find the file specified", errorcode: 80070002)。
我尝试的第一个 vbs:
Dim sAppPath
Dim sPrgFolder
sPrgFolder=CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")
sAppPath =sPrgFolder + "c:\UserGuide.doc"
WScript.CreateObject("WScript.Shell").Run sAppPath)
我尝试的第二个 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"
【问题讨论】:
-
您的第一个代码不起作用,因为您在
sPrgFolder中获得了程序文件夹路径,其中可能包含类似于C:\Program Files\Microsoft Office\OFFICE11的内容。然后添加C:\UserGuide.doc` to the variable, so you'll end up with:C:\Program Files\Microsoft Office\OFFICE11\C:\UserGuide.doc`,这根本行不通。不过,我已经没时间看第二个 vbs 了。
标签: vbscript ms-word office-interop