xlwings:Python 出错时终止 VBA 执行

问题描述 投票:0回答:1

我有一个 Excel VBA 子例程,它使用 xlwings

RunPython
命令调用 Python 函数。如果调用的 Python 函数因错误而终止,则 Excel 中会出现带有 Python 错误回溯的弹出窗口。然而,随后 Excel 愉快地继续执行 VBA 子例程的其余部分。

如何在

RunPython
中的 Python 函数引发错误后强制 VBA 子例程立即终止?

python excel vba xlwings
1个回答
0
投票
如果出现错误,

RunPython
返回 -1。因此解决方案是:

return_code = RunPython("import hello; hello.world()")
If return_code = -1 Then
    End
End If
© www.soinside.com 2019 - 2024. All rights reserved.