如何使用vb脚本打开word文件
谁能告诉我如何使用 vbs windows 脚本打开 word 文件。
我尝试了这两套vbs,但是即使文件存在于指定位置,也会显示Windows脚本主机错误(“系统找不到指定的文件”,错误代码: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"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
LittleBobbyTables 在他的评论中解释了为什么你的第一个示例不起作用。
至于您的第二个示例,它不起作用,因为您没有在 winword.exe 路径和文件路径之间插入任何空格,因此您的命令行如下所示:
无论如何,像这样硬编码 winword.exe 路径是不可靠的,因为该路径在 64 位和某些本地化 Windows 版本以及某些 MS Office 版本中是不同的。我建议您改用 Word 自动化对象:
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:
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:
尝试这个修改后的代码,检查最后一行的修改:)
try this revised code, check the modifications in last line :)
谢谢各位朋友......
我用这些vbs就可以了。
Thanks buddies.....
i got it working with these vbs.
这个怎么样?:
How about this?: