Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a File whose Name Contains a Space

Tags:

excel

vba

wsh

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

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

I get:

enter image description here

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

like image 666
Gary's Student Avatar asked Oct 05 '17 19:10

Gary's Student


People also ask

Can a file name contain spaces?

Don't start or end your filename with a space, period, hyphen, or underline. Keep your filenames to a reasonable length and be sure they are under 31 characters. Most operating systems are case sensitive; always use lowercase. Avoid using spaces and underscores; use a hyphen instead.

Do spaces in file names cause problems?

Avoid spaces A space in a filename can cause errors when loading a file or when transferring files between computers. Common replacements for spaces in a filenames are dashes (-) or underscores (_). Alternatively you can also use a technique called camelCase which uses letter case to separate name elements.

Can Linux file names have spaces?

It's not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces. While it is not recommended to have file names with spaces, let's discuss how to manage filename with spaces in a Linux system.


1 Answers

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

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"""
like image 181
YowE3K Avatar answered Oct 01 '22 23:10

YowE3K