0

I am executing a .py python script from VBA using the Shell()

Sub Run_Python()
    
    Dim PYTHON_FULL_PATH, PYTHON_SCRIPT_PATH As String   
    iRowNo = 2
    Do Until Sheet7.Cells(iRowNo, 1) = ""
        PYTHON_FULL_PATH = Sheet7.Cells(iRowNo, 1)
        PYTHON_SCRIPT_PATH = Sheet7.Cells(iRowNo, 2)
    
        iRowNo = iRowNo + 1
    Loop
    
    RetVal = Shell(PYTHON_FULL_PATH & " " & PYTHON_SCRIPT_PATH, vbNormalFocus)
    

End Sub

The Python script prints few lines as part of the code and those print statements are shown in the CMD prompt.

But once the execution completes or errored our the CMD gets closed automatically.

Is there a way to keep the CMD prompt visible even after the execution gets completed ?

Thanks,

Mofi
  • 46,139
  • 17
  • 80
  • 143
SM079
  • 412
  • 2
  • 7
  • 20
  • 1
    Have you tried the same commands you’d normally use in a command prompt? https://stackoverflow.com/a/17966906/2727437 – Marcucciboy2 Dec 14 '22 at 06:40

1 Answers1

2

Run cmd.exe with /k parameter:

RetVal = Shell("cmd /k " & PYTHON_FULL_PATH & " " & PYTHON_SCRIPT_PATH, vbNormalFocus)
artnib
  • 480
  • 4
  • 7