2

In VBA code, I am calling Python and passing an argument successfully like this when there are no spaces in the path to the python file:

Dim wsh As Object
Set wsh = CreateObject("WScript.Shell")

Dim shell_exec As Object
Set shell_exec = wsh.Exec("C:\path_to_executable\python.exe " & "C:\path_without_spaces\file.py " & arg)

However, I cannot find any way to pass an argument to python when the path to the python file contains spaces. For example, triple double-quoting the path works when there is no argument:

Set shell_exec = wsh.Exec("C:\path_to_executable\python.exe " & """C:\path with spaces\file.py""")

But I am unable to find any way to pass an argument when doing this. For example, calls like this (and every variation on this theme I can think of) fail:

Set shell_exec = wsh.Exec("C:\path_to_executable\python.exe " & """C:\path with spaces\file.py """ & arg)

Is this possible, or is the use of spaces fatally ambiguous in this situation?

Advice is appreciated.

7
  • 1
    You might be able to use Windows file associations and skip the part about the path to python.exe Commented Mar 30, 2016 at 19:57
  • @JohnColeman: Interesting. How could I do this while using the shell? It insists on a path to an executable as a argument. Commented Mar 30, 2016 at 20:15
  • I'm not sure how to use it from VBScript -- but it can work from the command prompt. Commented Mar 30, 2016 at 20:52
  • 1
    The Run method seems easier to use with arguments with spaces. I'm not sure why. Commented Mar 30, 2016 at 21:16
  • 1
    It seems to be a bit of a bug since exactly the same string will work with Run but fail with Exec. How badly do you need to have spaces in the path? Personally, I keep most of my programs in a folder in my documents (in a path which contains spaces) but also keep a folder close to the root directory for programs that I need to invoke from the command line (which isn't a very common use-case for me). You could create such a folder and move a copy of your script to it. Commented Mar 31, 2016 at 12:29

1 Answer 1

1

Try to use multiple quotes:

Set shell_exec = wsh.Exec("""C:\path_to_executable\python.exe """ & """C:\path with spaces\file.py """ & arg)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, hadn't tried that one, but no success. This causes the terminal window to stay open, which is different, but the argument is still not passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.