Paul
Paul

Reputation: 982

Calling python script from VBA

There seems to be a lot of threads on this one and they all point to this solution:

Function call_py()

Dim rv As Variant
Const pypath = "C:\Users\user\AppData\Local\Programs\Python\Python36-32\python.exe "
Const scriptpath = "c:\Users\user\Desktop\test.py"

    rv = Shell(pypath & scriptpath, vbNormalFocus)


End Function

or something similar. All this does though is make a shell pop up and disappear without executing the script. What am I doing wrong?

P.

Upvotes: 2

Views: 954

Answers (2)

Florent B.
Florent B.

Reputation: 42518

Add the argument "-i" to inspect the execution.

The console will remain opened and you'll be able to see why it's not working as expected :

Shell "python -i " & scriptpath

And make sure that the paths are quoted if they contain a space character.

Upvotes: 0

Tehscript
Tehscript

Reputation: 2556

Try this:

Sub bat()
Dim batch, fso As Object
Dim path As String
path = "c:\Users\user\Desktop\"

Set fso = CreateObject("Scripting.FileSystemObject")
Set batch = fso.CreateTextFile(path & "python.bat", True)
batch.writeline "cd " & path
batch.writeline "python test.py"
batch.Close

Shell path & "python.bat", 1
End Sub

Upvotes: 1

Related Questions