Call python script from vba with wsh
<p>I need to call a python script from vba which works fine by using the shell.</p>

<pre><code>Sub CallPythonScript()

        Call Shell("C:\Program Files (x86)\Python27\python.exe C:\Users\Markus\BrowseDirectory.py")

End Sub
</code></pre>

<p>But when I try using wsh (because of the wait funcionality) it just won't work anymore. </p>

<pre><code>Sub CallPythonScript()

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

    Dim myApp As String: myApp = "C:\Program Files (x86)\Python27\python.exe C:\Users\Markus\BrowseDirectory.py"
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1

    wsh.Run """"" &amp; myApp &amp; """"", windowStyle, waitOnReturn

End Sub
</code></pre>

<p>However, I used the same code at home and everything worked out just fine, with the difference that there weren't any blanks in the path. So naturally there must be something wrong with the blanks. Help is greatly appreciated. </p>

<p>The interpreter cannot possibly distinguish directories containing spaces from real arguments unless you protect the directories with quotes.</p>

<p>A workaround, which is actually a better solution, relies on the fact that Python associates <code>.py</code> extension with the installed python interpreter.</p>

<p>Just do this (like you would launch a <code>.bat</code> file):</p>

<pre><code>Dim myApp As String: myApp = "C:\Users\Markus\BrowseDirectory.py"
</code></pre>

<p>and your code will work no matter where python is installed (and the problem with spaces vanishes)</p>

<p>This little test in excel worked for me and ran <code>pycrust.py</code> which happens to be in my system <code>PATH</code>:</p>

<pre><code>Sub foo()
  Dim wsh As Object
  Set wsh = VBA.CreateObject("WScript.Shell")

    Dim myApp As String: myApp = "pycrust.py"
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1

    wsh.Run myApp, windowStyle, waitOnReturn

End Sub
</code></pre>

<p>(I had to simplify your <code>wsh.Run</code> line because it was not working, I suspect that the 256 quotes you added didn't do any good)</p>

python vba excel-vba wsh