英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论