xlwings:在Python错误发生时终止VBA执行。

huangapple go评论102阅读模式
英文:

xlwings: terminate VBA execution upon Python error

问题

我有一个Excel VBA子程序,它使用xlwings的RunPython命令调用一个Python函数。如果被调用的Python函数由于错误而终止,那么Excel中会出现一个带有Python错误回溯的弹出窗口。然而,在此之后,Excel会继续执行剩余的VBA子程序。

我该如何在RunPython的Python函数引发错误后,强制VBA子程序立即终止?

英文:

I have an Excel VBA subroutine that calls a Python function with the xlwings RunPython command. If the called Python function terminates due to an error, then a pop-up with the Python error traceback appears in Excel. However afterwards Excel happily continues with execution of the remainder of the VBA subroutine.

How can I force the VBA subroutine to terminate immediately after the Python function from RunPython raises an error?

答案1

得分: 0

RunPython在出现错误时返回-1。因此,解决方案如下:

return_code = RunPython("import hello; hello.world()")
if return_code == -1:
    End
英文:

RunPython returns -1 in case of an error. Therefore a solution is:

return_code = RunPython("import hello; hello.world()")
If return_code = -1 Then
    End
End If

huangapple
  • 本文由 发表于 2023年8月9日 15:31:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定