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

发布于 2024-09-25 19:52:59 字数 757 浏览 6 评论 0原文

谁能告诉我如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仲春光 2024-10-02 19:52:59

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"

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"
思慕 2024-10-02 19:52:59
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"

尝试这个修改后的代码,检查最后一行的修改:)

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

人间不值得 2024-10-02 19:52:59

谢谢各位朋友......
我用这些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

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
笑看君怀她人 2024-10-02 19:52:59

这个怎么样?:

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

WshShell.AppActivate "Word"

How about this?:

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

WshShell.AppActivate "Word"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文