0

I have Python code that creates an image (jpg format). This code works in Jupyter and Visual Studio code.

I'm trying to execute the code from the Excel workbook that the code imports the data. The idea is to have a button in the Excel workbook to execute the Python code and obtain the image.

Code to execute the Python code in VBA:

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

It doesn't show me errors. It appears in the console for one second and closes.

I expect that when I use the button in the Excel workbook, an image appears.

2
  • Check python with simple program objShell.Run PythonExePath & " -c ""import os;print(os.getcwd());input()""" Commented Mar 29, 2023 at 16:26
  • Maybe try 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. Commented Mar 29, 2023 at 18:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.