私信  •  关注

PCG

PCG 最近回复了
3 年前
回复了 PCG 创建的主题 » 为什么VB外壳函数不停止Python代码执行窗口?

该问题与路径中的空间有关。python路径中的空格可以,但是python脚本路径不应该有任何空格。当有空格时,它会被分解成单独的变量,并且永远不会将脚本视为文件。因此,解决方法是对带有空格的路径使用双引号。

VB脚本应如下所示:

Sub run_python()

python_exe = "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\python.exe"
python_script = "C:/Users/Goerge/OneDrive - MS Corporation/Documents/Development/python-excel/excel_python.py"

If Dir(python_exe) = "" Then
    MsgBox ("Python executabl does not exists")
End If

If Dir(python_script) = "" Then
    MsgBox ("Python script does not exists")
End If

python_code = python_exe & " " & """" & python_script & """"
MsgBox python_code
RetVal = Shell(python_code)

'Shell "pskill " & " " & RetVal

End Sub