我有一个 Excel VBA 子例程,它使用 xlwings
RunPython
命令调用 Python 函数。如果调用的 Python 函数因错误而终止,则 Excel 中会出现带有 Python 错误回溯的弹出窗口。然而,随后 Excel 愉快地继续执行 VBA 子例程的其余部分。
如何在
RunPython
中的 Python 函数引发错误后强制 VBA 子例程立即终止?
RunPython
返回 -1。因此解决方案是:
return_code = RunPython("import hello; hello.world()")
If return_code = -1 Then
End
End If