打开一个名称包含空格的文件 [英] Opening a File whose Name Contains a Space
问题描述
我有一些简单的Excel VBA代码,可以打开非Excel文件,例如:
I have some simple Excel VBA code that opens non-Excel files like:
Sub scriptTest()
Set objshell = CreateObject("Wscript.Shell")
objshell.Run ("C:\TestFolder\Book1.pdf")
Set objshell = Nothing
End Sub
运行此命令将在Acrobat Reader中打开文件.但是,如果我尝试打开一个名称包含空格字符的文件,例如:
Running this opens the file in the Acrobat Reader. However if I try to open a file whose name contains a space character like:
Sub scriptTest()
Set objshell = CreateObject("Wscript.Shell")
objshell.Run ("C:\TestFolder\Bo ok1.pdf")
Set objshell = Nothing
End Sub
我得到:
如果我使用Windows开始"菜单中的Run
命令,则两个文件都可以正常打开.我该如何克服这个问题?
Both files open fine if I use the Run
command from the Windows Start menu. How can I overcome this problem ??
推荐答案
在执行语句objshell.Run ("C:\TestFolder\Bo ok1.pdf")
时,您正在要求Shell执行命令
When executing the statement objshell.Run ("C:\TestFolder\Bo ok1.pdf")
, you are asking the shell to execute the command
C:\TestFolder\Bo ok1.pdf
这被解释为使用参数ok1.pdf
执行程序C:\TestFolder\Bo.exe
的请求.
This is interpreted as being a request to execute the program C:\TestFolder\Bo.exe
with a parameter of ok1.pdf
.
您实际上希望外壳程序执行命令
You actually want the shell to execute the command
"C:\TestFolder\Bo ok1.pdf"
命令解释器使用引号将命令的各个部分组合"在一起.
where the quotation marks are used by the command interpreter to "group" parts of the command together.
要获取该命令,您需要执行以下语句
To obtain that command, you need to execute the statement
objshell.Run """C:\TestFolder\Bo ok1.pdf"""
这篇关于打开一个名称包含空格的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!