How to open an .exe file using vbscript that has spaces in the name?

Posted on 2014-12-04
Medium Priority
Last Modified: 2014-12-04
All with the following I can get my .exe file opened up except when there are spaces in the name.
Please note the following below:

Because the program PDF Parser.exe has a space it never seems to find the file to open and run it.
How do I change the syntax below to make this work?  

I have tried several samples shown here:  


Is there another I can try?  
Does someone have an .exe file to try and open - perhaps rename it with a space and try the following code below:

sExecutable = "C:\Users\slecompte\Documents\SMW_Transfer\PDF Parser\PDF Parser.exe"

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )

If LikeFunction(sExecutable) = False Then
  'this line works when a file has no spaces.
  objShell.Run "%comspec% /c start " & sExecutable, 0, True
  'this line doesn't work no matter what I try:
  Set objShell = WScript.CreateObject("WScript.Shell")
  strCommand = "%comspec% /c start "& chr(34) & sExecutable & chr(34)
  'sExecutable = chr(34) & chr(34) & chr(34) & sExecutable & chr(34) & chr(34) & chr(34)
  'MsgBox sExecutable
'"""C:\Program Files\abc_edf\cde\qw.exe""",1,true
  objShell.Run strCommand , 1, True
  'MsgBox strCommand
End if
Set objShell = Nothing

Public Function LikeFunction(sValue)

  Dim bValue
  Dim i
  For i = 1 to len(sValue)
     If mid(sValue, i, 1) = " " Then
       bValue = True
       Exit For
     End If
  LikeFunction = bValue

End Function

Open in new window

Question by:stephenlecomptejr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 57

Expert Comment

by:Bill Prew
ID: 40481495
This should do it.

Since you are using the START command with a quoted file name, you need to include the title paramater.  So the effect you are going for is:

start /c "" "C:\Users\slecompte\Documents\SMW_Transfer\PDF Parser\PDF Parser.exe"

Open in new window

which you can code as:

  strCommand = "%comspec% /c start " & chr(34) chr(34) & " " & chr(34) & sExecutable & chr(34)
  objShell.Run strCommand , 1, True

Open in new window

LVL 85

Accepted Solution

oBdA earned 2000 total points
ID: 40481532
You don't need cmd.exe when starting an exe file. This should do the job just fine:
sExecutable = "C:\Users\slecompte\Documents\SMW_Transfer\PDF Parser\PDF Parser.exe"
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
strCommand = chr(34) & sExecutable & chr(34) &
objShell.Run strCommand , 1, True
Set objShell = Nothing

Open in new window


Author Comment

ID: 40481600
oBdA,  Your code worked even though - I swear I tried that exact code.  Of course I had to take the & at the end off.
Bill, I tried your code but it gave an error - expected end of statement - Line 13 - char: 48
which was this line:
  strCommand = "%comspec% /c start " & chr(34) chr(34) & " " & chr(34) & sExecutable & chr(34)
LVL 57

Expert Comment

by:Bill Prew
ID: 40481865
Whoops, forest, tress, etc.  I was focused in on the file you were executing, etc.  Glad you have a working solution.


Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question