如果 DoCmd.SendObject 过早终止则错误捕获

Error Catch if DoCmd.SendObject is terminated prematurely

我在 MSAccess VBA 中有一个电子邮件 (SendObject) 例程。如果用户决定不提交点击发送按钮并简单地关闭电子邮件表单,代码就会崩溃。

Private Sub ButtonSupportEmail_Click()

Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant

varName = "somebody@gmail.com"
varCC = "somebody@gmail.com"

varSubject = "ADB Front End Client Support_" & Now()
varBody = "Dear Amazing ADB Support Team:"
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False

End Sub

我可以添加一个错误捕获提示(通过弹出表单)用户“未发送电子邮件”并正常关闭吗?

图 A:用户关闭 Outlook 电子邮件表单时出错

当然 - 使用 On Error Resume next 并检查 Err.NumberErr.Description 来捕获和处理异常。

有关详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/on-error-statement

添加错误处理并忽略 2501 - 操作取消错误。

Private Sub ButtonSupportEmail_Click()
    On Error GoTo Trap

    Dim varName As Variant
    Dim varCC As Variant
    Dim varSubject As Variant
    Dim varBody As Variant

    varName = "somebody@gmail.com"
    varCC = "somebody@gmail.com"

    varSubject = "ADB Front End Client Support_" & Now()
    varBody = "Dear Amazing ADB Support Team:"
    DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False

Leave:
    Exit Sub

Trap:
    If Err.Number <> 2501 Then MsgBox Err.Description, vbCritical
    Resume Leave
End Sub