SM079
SM079

Reputation: 713

VBA: Shell() command to keep CMD after execution completes

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,

Upvotes: 0

Views: 450

Answers (1)

artnib
artnib

Reputation: 508

Run cmd.exe with /k parameter:

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

Upvotes: 2

Related Questions