3

So I have a function that allows the user to email an updated picture for an asset which works fine and good except if the user closes the email before sending. I have an error handler set up but it doesn't seem to capture the error. Here is my function code:

Function Email()
Globals.Logging "Opened Email for updating picture"
On Error GoTo ErrorHandler:
Dim strTagNumber As String
strTagNumber = Me.txtTagNumber.Value

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

varName = "[email protected]"
varCC = ""
varSubject = "Updated Picture for Asset Number " & strTagNumber
varBody = "Sent by MS Access"

DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
Globals.Logging "Sent Email"

Cleanup:
  varName = Nothing
  varCC = Nothing
  varSubject = Nothing
  varBody = Nothing
  Exit Function

ErrorHandler:
  Select Case Err.Number
    Case 2501
      MsgBox "Email message was Cancelled."
      Globals.Logging "Canceled Email"
    Case Else
      MsgBox Err.Number & ": " & Err.Description
      Globals.Logging "Email Error " & Err.Number & ": " & Err.Description
  End Select
  Resume Cleanup
End Function

Any help would be appreciated. Thank you in advance.

10
  • if you take this out - On Error GoTo ErrorHandler: which line does it stop on? Commented Aug 4, 2017 at 16:28
  • Is there even an error to catch? BTW the first thing you do in CleanUp is Exit Function? Commented Aug 4, 2017 at 16:35
  • It only throws the error if the user closes the email before sending which is what the error handler is designed to handle. The error is Run-time Error '2501' "The SendObject action was canceled". Commented Aug 4, 2017 at 18:25
  • 1
    What do you mean by it "doesn't seem to capture the error" and then later "it throws the error"? Do you mean it shows the pop-up message but then continues with the VBA code without ever going to the ErrorHandler? Sorry to be picky about words, but to me "throwing the error" actually means it causes the error handling code to execute (not just showing a pop-up). Commented Aug 5, 2017 at 17:18
  • 1
    Ok I figured it out, I had the options in the vba editor set to break on all errors. I changed it to break on unhandled errors and the error handler works now. Thanks for your help. Commented Aug 10, 2017 at 20:01

2 Answers 2

1

As described in online documentation, DoCmd.SendObjects

... uses the Mail Applications Programming Interface (MAPI)

In other words, Access (or Excel) does not actually have its own email capability. It is dependent upon a properly installed and configured MAPI email client. Unless you have purposefully installed and setup another default email client on Windows, the default is likely Outlook if you have it installed with MS Office. Windows email clients have changed with the many version of Windows, but the default might also be a simple Windows email client.

It is very likely that the MAPI client could be showing the error message, then not actually throwing/raising the error before it returns program flow back to the VBA module which initiated the call.

I recall being aware at some point of an Outlook setting that dictated certain behavior for MAPI and/or COM Automation interfaces, whether it showed errors or not. I usually would not throw out such wishy-washy info on Stack Overflow before verifying, but from what I see of the discussion around this issue, nobody really addresses this aspect of SendObjects.

Besides using Automation to send email via Outlook as other have suggested, you could inspect the Windows default email client settings. Perhaps test another email client to see if you get different results.

Sign up to request clarification or add additional context in comments.

Comments

0

Tested on Access 2016. The error is captured and I can see the message box saying "Email message was Cancelled.".

Maybe you can try to use Outlook object for sending the email too.

1 Comment

Hmmm, that is definitely weird since it is still not working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.