-1

我在 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 电子邮件表单时出错
在此处输入图像描述

4

2 回答 2

2

添加错误处理并忽略 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
于 2021-07-28T13:33:11.847 回答
0

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

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

于 2021-07-28T05:12:36.277 回答