打开名称包含空格的文件

时间:2022-12-19 22:38:04

I have some simple Excel VBA code that opens non-Excel files like:

我有一些简单的Excel VBA代码,可以打开非Excel文件,如:

Sub scriptTest()
    Set objshell = CreateObject("Wscript.Shell")
    objshell.Run ("C:\TestFolder\Book1.pdf")
    Set objshell = Nothing
End Sub

Running this opens the file in the Acrobat Reader. However if I try to open a file whose name contains a space character like:

运行此命令会在Acrobat Reader中打开该文件。但是,如果我尝试打开一个名称包含空格字符的文件,例如:

Sub scriptTest()
    Set objshell = CreateObject("Wscript.Shell")
    objshell.Run ("C:\TestFolder\Bo ok1.pdf")
    Set objshell = Nothing
End Sub

I get:

我明白了:

打开名称包含空格的文件

Both files open fine if I use the Run command from the Windows Start menu. How can I overcome this problem ??

如果我使用Windows“开始”菜单中的“运行”命令,则两个文件都打开正常。我怎么能克服这个问题?

1 个解决方案

#1


8  

When executing the statement objshell.Run ("C:\TestFolder\Bo ok1.pdf"), you are asking the shell to execute the command

执行语句objshell.Run(“C:\ TestFolder \ Bo ok1.pdf”)时,您要求shell执行命令

C:\TestFolder\Bo ok1.pdf

This is interpreted as being a request to execute the program C:\TestFolder\Bo.exe with a parameter of ok1.pdf.

这被解释为执行程序C:\ TestFolder \ Bo.exe的请求,参数为ok1.pdf。

You actually want the shell to execute the command

您实际上希望shell执行命令

"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"""

#1


8  

When executing the statement objshell.Run ("C:\TestFolder\Bo ok1.pdf"), you are asking the shell to execute the command

执行语句objshell.Run(“C:\ TestFolder \ Bo ok1.pdf”)时,您要求shell执行命令

C:\TestFolder\Bo ok1.pdf

This is interpreted as being a request to execute the program C:\TestFolder\Bo.exe with a parameter of ok1.pdf.

这被解释为执行程序C:\ TestFolder \ Bo.exe的请求,参数为ok1.pdf。

You actually want the shell to execute the command

您实际上希望shell执行命令

"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"""