Opening a file whose name contains a space

I have a simple Excel VBA code that opens files without Excel, for example:

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

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

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

I get:

enter image description here

Both files open normally if I use the Run command in the Windows Start menu. How can I solve this problem?

+5
source share
1 answer

When executing the objshell.Run ("C:\TestFolder\Bo ok1.pdf") you request the shell command

 C:\TestFolder\Bo ok1.pdf 

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

Do you really want the shell to execute the command

 "C:\TestFolder\Bo ok1.pdf" 

where quotation marks are used by the shell to group parts of the command.

To get this command you need to execute a statement

 objshell.Run """C:\TestFolder\Bo ok1.pdf""" 
+8
source

Source: https://habr.com/ru/post/1272372/


All Articles