我正在写VBA代码的错误处理。
我想让它在代码失败时给我发一封电子邮件:
On Error GoTo x:
'main code
Exit Sub
x:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "hamza.ali@telus.com"
.Subject = "Error Occured - Error Number " & Err.Number
.Body = "We have found an error with the bot. Please open the VM to debug the problem. -->" & Err.Description & " And the reason is: " & Err.Source
.Display '~~> Change this to .Send for sending the email
End With
Debug.Print "x -" & Err.Description & Err.Source
Set OutApp = Nothing: Set OutMail = Nothing
End Sub
我想通过这个错误处理方法发送一封电子邮件,然后返回到错误行并停止。当我试图调试时,我会知道错误发生在哪一行,而不需要运行100行代码来查看错误发生在哪里。
1条答案
按热度按时间unhi4e5o1#
Stop
行的错误处理程序中停止