Error Handler Not Working in VBA Access 2013 Function
So I have a function that allows the user to send an updated picture for an asset, which works great and well, except when the user closes the email before sending. I have an error handler installed, but it doesn't seem to be catching 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 = "myAnon@email.test"
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 greatly appreciated. Thank you in advance.
source to share
As described in the online documentation , DoCmd.SendObjects
... uses the Mail Application Programming Interface (MAPI)
In other words, Access (or Excel) doesn't actually have email capabilities of its own. This depends on a properly installed and configured MAPI email client. Unless you intentionally installed and configured a different default email client on Windows, the default is most likely Outlook if it is installed on MS Office. Windows email clients have changed with many versions of Windows, but the default can also be a plain Windows email client.
It is very likely that the MAPI client might display an error message and not actually throw / raise an error before it returns the program flow back to the VBA module that initiated the call.
I remember that at some point an Outlook parameter was known that dictated certain work for the MAPI and / or COM Automation interfaces, whether they were showing errors or not. I usually didn't throw this kind of stamina information on Stack Overflow before checking out, but from what I see in the discussion of this issue, nobody is addressing this aspect of SendObjects.
Apart from using the Automate feature to send email through Outlook as others have suggested, you can check your Windows default email client settings. Maybe check with another email client to see if you have different results.
source to share