-1

I have a email (SendObject) routine in MSAccess VBA. The code crashes if the user decides not to commit to hit the send button, and simply closes the email form.

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

Is there an error catch I can add that prompts (via a popup form) the user "No email sent" and closes gracefully?

Fig A: Error from user closing Outlook Email Form
enter image description here

Community
  • 1
  • 1
NanoNet
  • 218
  • 3
  • 11

2 Answers2

2

Add error handling and ignore the 2501 - Operation cancelled error.

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
Kostas K.
  • 8,293
  • 2
  • 22
  • 28
0

Of course - use On Error Resume next and check Err.Number and Err.Description to catch and process the exception.

See https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/on-error-statement for more information.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I was more looking for a snippet that could help me along the way as opposed to a "google it" answer. I was pretty close but needed a slight nudge in the right direction. – NanoNet Aug 04 '21 at 00:55