Home > front end >  VBA: Shell() command to keep CMD after execution completes
VBA: Shell() command to keep CMD after execution completes

Time:12-14

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,

CodePudding user response:

Run cmd.exe with /k parameter:

RetVal = Shell("cmd /k " & PYTHON_FULL_PATH & " " & PYTHON_SCRIPT_PATH, vbNormalFocus)
  • Related