Reputation: 33
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.
Upvotes: 3
Views: 187
Reputation: 3886
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.
Upvotes: 1
Reputation: 116
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.
Upvotes: 0