我通过VBA宏启动Python。如果Python路径不好,我想要一个正确的错误消息。
我在2台不同的计算机上使用了此宏,而Python路径在这两台计算机上并不相同。 2条路径位于2个不同的单元格上,其名称分别为path_Python和path_Python_2
这是我的代码:
Function launchPython() As Boolean
Dim pathScript As String
pathScript = [path_files] + "code.py"
On Error GoTo err
Shell [path_Python].Value & " " & pathScript
launchPython = True
Exit Function
err:
On Error GoTo err2
Shell [path_Python_2].Value & " " & pathScript
launchPython = True
err2:
launchPython = False
MsgBox "Error: please check Python paths"
End Function
问题是,当2的路径不好时,我没有在VBA错误消息上阻塞,而是去了err2。
err:
On Error GoTo err2
Shell [path_Python_2].Value & " " & pathScript
我该如何解决?
非常感谢你
最佳答案
我将尝试检查文件是否存在并尝试尝试,而不是尝试将命令添加到可能缺少的路径中。您可以使用许多功能之一,例如http://allenbrowne.com/func-11.html中的FileExists,或仅检查dir $返回非空白。
Dim Path as string
path = path_python
if dir(path) = "" then
path = path_python2
if dir(path) = "" then err.raise
end if
shell path & " " & pathScript
关于vba - VBA错误处理,将错误定义为错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16415280/