任何人都可以告诉我如何使用vbs windows脚本打开word文件。
我尝试了这两组vbs,但是当脚本存在于指定位置时,会显示Windows脚本主机错误("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"
答案 0 :(得分:7)
LittleBobbyTables在他的评论中解释了为什么你的第一个例子不起作用。
至于你的第二个例子,它不起作用,因为你没有在winword.exe路径和文件路径之间插入任何空格,所以你的命令行如下所示:
"C:\Program Files\Microsoft Office\Office\winword.exe""C:\UserGuide.doc"
无论如何,像这样硬编码winword.exe路径是不可靠的,因为这个路径在64位和一些本地化Windows版本以及某些MS Office版本中是不同的。我建议你改用Word自动化对象:
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.Open "C:\UserGuide.doc"
答案 1 :(得分:1)
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"
尝试修改后的代码,检查最后一行的修改:)
答案 2 :(得分:0)
谢谢伙计..... 我得到了这些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