I am trying to call a Python script on VBA, and I am new to this.
I tried converting the main script to an EXE file using py2exe and then calling it from VBA (shell), but the main script calls other scripts, and therefore it becomes complicated and I messed it up (my EXE file is not functional). Besides, the the main script is a large file and I do not want to revise it a lot.
Bottom line, is there a way to call the main script from Excel VBA, without converting the script to an EXE file?
So far, I tried:
RetVal = Shell("C:\python27\python.exe " & "import " & "C:\\" & "MainScriptFile")
It starts python.exe, but it doesn't do anything else. Then I tried:
RetVal = Shell("C:\Windows\System32\cmd.exe " & "python " & "C:\\Python27\\hello.py")
It starts a command prompt, but it does not even start the Python interpreter.
P.S.: I checked all the related questions in the forum, they do not solve my problem.
Try this:
RetVal = Shell("<full path to python.exe> " & "<full path to your python script>")
Or if the Python script is in the same folder as the workbook, then you can try:
RetVal = Shell("<full path to python.exe> " & ActiveWorkBook.Path & "\<python script name>")
All details within <>
are to be given. <>
- indicates changeable fields
I guess this should work. But then again, if your script is going to call other files which are in different folders, it can cause errors unless your script has properly handled it.