0

I have tried several solutions to run python script out of VBA.

My current solution that still doesnt work

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim windowStyle As Integer: windowStyle = 1
Dim waitOnReturn As Boolean: waitOnReturn = True

wsh.Run "C:\Python33\python.exe C:\Users\***\Documents\Promo models\get_sku_data.py", windowStyle, waitOnReturn

Any ideas?

paveltr
  • 474
  • 1
  • 8
  • 22
  • This seems to be a VBA question, not a Python question. The trouble is with starting the Python interpreter process, not with the script itself. Also, why would you do this? ;) – advance512 Jun 21 '16 at 14:50
  • I was asked to directly present my model results on Excel sheet for business people – paveltr Jun 21 '16 at 14:55
  • Great, there are various Python libraries for modifying Excel sheets. No need to run Python via VBA.. – advance512 Jun 21 '16 at 14:57
  • 1
    Yeah, of course, but the problem is that I have to make it as a Excel-button solution =) – paveltr Jun 21 '16 at 15:06
  • "Run" paths with spaces should likely be quoted. – Tim Williams Jun 21 '16 at 16:11
  • Does this answer your question? [Run and execute a python script from VBA](https://stackoverflow.com/questions/30531662/run-and-execute-a-python-script-from-vba) – Graham Monkman Jun 19 '23 at 14:46

3 Answers3

0

xlwings claims to make python from excel very easy:

Sub RandomNumbers()
    RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub

To make this run, just import the VBA module xlwings.bas in the VBA editor (Open the VBA editor with Alt-F11, then go to File > Import File... and import the xlwings.bas file. ). It can be found in the directory of your xlwings installation.

syntonym
  • 7,134
  • 2
  • 32
  • 45
  • I would like not to use VBA extensions cause I need to call python script on other computers which will require Excel changes many times. Any way to call python script with default libs? – paveltr Jun 21 '16 at 14:54
  • So excel runs on computer A but you need to call a python script on Computer B? For excel without xlwings I'm out of my knowledge. – syntonym Jun 21 '16 at 14:57
  • No, it will work on different but same computers. On computer A both script and Excel will work together, the same config on computer B and etc. I just dont want to install this lib everywhere if possible – paveltr Jun 21 '16 at 14:59
  • Ah okay. Maybe wsad597 answer can help you then. – syntonym Jun 21 '16 at 15:01
0

Finally I ended up with creation of .exe files instead of trying to call scripts itself.

So, I applied cx_Freeze, created .exe files and then called them as

Script_path1 = "C:\Users\" + Environ("Username") & "\Documents\Promo_models\get_sku_data\gui_changed.exe"
wsh.Run Script_path1, windowStyle, waitOnReturn
paveltr
  • 474
  • 1
  • 8
  • 22
0

Here are a couple options to try.

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>")
ASH
  • 20,759
  • 19
  • 87
  • 200