0

I have a python code that create a image (jpg format). This code works in jupyter and visual studio code. I'm trying to execute the code from the same excel that the code import the data. The idea is to have a button in the excel to execute the python code and obtain the image.

The code that I'm using to execute the python code in VBA is:

Sub RunPythonScript_1()

Dim objShell As Object
Dim PythonExePath, PythonScriptPath As String

    Set objShell = VBA.CreateObject("Wscript.Shell")
    
    PythonExePath = """C:\Users\227871\AppData\Local\Microsoft\WindowsApps\python.exe"""
    PythonScriptPath = """C:\Users\227871\OneDrive - Weir Group PLC\Francisca\Mejora App Weir\Balance\Balance Collahuasi\Balance 1.py"""
    
    objShell.Run PythonExePath & PythonScriptPath
    
End Sub

When I run the code, it doesn't show me errors. Just appears the console one second and after it closes.

Please, could you help me with this??

I expected that when I use the button in excel, a image appears.

CDP1802
  • 13,871
  • 2
  • 7
  • 17
  • Check python with simple program `objShell.Run PythonExePath & " -c ""import os;print(os.getcwd());input()"""` – CDP1802 Mar 29 '23 at 16:26
  • Maybe try https://stackoverflow.com/a/73564937/478884 and see if there's any error in your script. Sometimes if the script assumes a certain active directory when run, it may error if called from VBA and the directory is not set to the expected value. – Tim Williams Mar 29 '23 at 18:27

0 Answers0