Solved Catch 2501 Error Message (DoCmd.SendObject) Not Working (1 Viewer)

Jason Lee Hayes

Active member
Local time
Today, 09:50
Joined
Jul 25, 2020
Messages
174
Hi,

I am exporting a report as PDF and it works fine using the DoCmd.SendObject method...
Report emails fine however if i cancel the outlook message and do not send i get the attached error as expected..
Anticipating this i thought my code would identify the code 2501 and ignore it (Ignore in that when i click cancel the outlook message closes and i am returned to the form. At the moment the message closes and the form is presented but cannot click on anything and have to force a TSR shutdown of the MS application.

I'm sure its something simple - a bit like me simple lol

Thanks in advance...

Code:
Private Sub cmdEmail_Click() 'Output selected report as PDF and email
    
On Error GoTo Error_Handler

  DoCmd.SetWarnings False
 
    DoCmd.SendObject acSendReport, Me.lstQueries, acFormatPDF, _
    "toemailaddress@outlook.com; andemailaddress@outlook.com", _
    , "bccemailaddress@outlook.com", "Subject Here", "Message Body Here", False
 
  DoCmd.SetWarnings True

exitMe:
    On Error GoTo 0
    DoCmd.Hourglass False
    Exit Sub

Error_Handler_Exit:
    DoCmd.Hourglass False
    On Error Resume Next
    Exit Sub

Error_Handler:
   If Err = 2501 Then
        ' Report was cancelled, we can ignore this error
    Else

    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
        "Error Number: " & Err.Number & vbCrLf & _
        "Error Source: frmPrintReport/cmdemail_Click()" & vbCrLf & _
        "Error Description: " & Err.Description, vbCritical, _
        "An Error has Occurred!"
    Resume Error_Handler_Exit

End If
End Sub
 

Attachments

  • Cancal Email Send Error.jpg
    Cancal Email Send Error.jpg
    55.8 KB · Views: 428

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:50
Joined
May 7, 2009
Messages
19,169
maybe add code to the Form's On Error Event:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Response = acDataErrContinue
End Sub
 

Jason Lee Hayes

Active member
Local time
Today, 09:50
Joined
Jul 25, 2020
Messages
174
maybe add code to the Form's On Error Event:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Response = acDataErrContinue
End Sub
Jeez, i only posted a minute ago.. Super quick response that was :D

Thanks.. I understand what you have posted and added the code above to the forms on orror property however the result is still the same... when i am taken back to the form anything i click or any key a press i get the default beep sound. I have attached my VB error check settings; googled the issue before posting as i always do and i recall someone mentioning these setting may affect the outcome but not sure..

If i try to close the application (Top Right) then i get the Message On Trying To Close (Attached)
 

Attachments

  • VBA Settings.jpg
    VBA Settings.jpg
    80.3 KB · Views: 204
  • Message On Trying To Close Application.jpg
    Message On Trying To Close Application.jpg
    38.2 KB · Views: 425
Last edited:

Minty

AWF VIP
Local time
Today, 09:50
Joined
Jul 26, 2013
Messages
10,353
Shouldn't the code be

If Err.Number = 2501 Then
' Report was cancelled, we can ignore this error
Else
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:50
Joined
May 7, 2009
Messages
19,169
the Default member of Err Object is Number.
so:

If Err ...
If Err.Number

is the same.
 

Minty

AWF VIP
Local time
Today, 09:50
Joined
Jul 26, 2013
Messages
10,353
@arnelgp - I never new that!

@OP - remove the
Set Warning = False
It will suppress all the error messages.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:50
Joined
May 7, 2009
Messages
19,169
check this code:
Code:
Private Sub cmdEmail_Click() 'Output selected report as PDF and email
    
On Error GoTo Error_Handler

  DoCmd.SetWarnings False
 
    DoCmd.SendObject acSendReport, Me.lstQueries, acFormatPDF, _
    "toemailaddress@outlook.com; andemailaddress@outlook.com", _
    , "bccemailaddress@outlook.com", "Subject Here", "Message Body Here", False
 


Error_Handler_Exit:
   DoCmd.SetWarnings True
    DoCmd.Hourglass False
    Exit Sub

Error_Handler:
   If Err = 2501 Then
        ' Report was cancelled, we can ignore this error
    Else

    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
        "Error Number: " & Err.Number & vbCrLf & _
        "Error Source: frmPrintReport/cmdemail_Click()" & vbCrLf & _
        "Error Description: " & Err.Description, vbCritical, _
        "An Error has Occurred!"

End If
Resume Error_Handler_Exit
End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:50
Joined
Sep 12, 2006
Messages
15,613
Is there an error at all, if you cancel the email send?
Maybe it just assumes it worked, and doesn't care if it didn't.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:50
Joined
Oct 29, 2018
Messages
21,357
Hi. Just curious, how could you cancel the email message if the Edit Message argument is False?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:50
Joined
May 7, 2009
Messages
19,169
cancel the email message if the Edit Message argument is False?
he close Outlook without sending, that is causing the error.
test it to see.
 

Jason Lee Hayes

Active member
Local time
Today, 09:50
Joined
Jul 25, 2020
Messages
174
Hi,

Just to be clear...

See attached...

If a choose [Allow]; works perfect... If i click [Deny] i get the additional message on which i am forced to click [OK} after clicking {OK} message box disappears and form is shown but is not responsive and only beeping when you type or click.
 

Attachments

  • Outlook Allow Deny.jpg
    Outlook Allow Deny.jpg
    259.3 KB · Views: 318
  • Message When Clicking Deny.jpg
    Message When Clicking Deny.jpg
    258.9 KB · Views: 240
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 02:50
Joined
Oct 29, 2018
Messages
21,357
he close Outlook without sending, that is causing the error.
test it to see.
Sorry, I can't test it right now (I will later). I am just going by what the documentation says.


If the EditMessage argument is False, then there should be nothing to cancel, because Outlook will automatically send the message without the user even seeing it on the screen. That's my understanding of it, unless I misinterpreted how it's supposed to work. I know I've used that argument before, but I mostly set it to True or nothing/empty.

Cheers!
 

Jason Lee Hayes

Active member
Local time
Today, 09:50
Joined
Jul 25, 2020
Messages
174
Hi,

Yes, i understand what you are saying, if False then it should simply open outlook and send the message without even presenting the email message..:unsure: To be fair i don't really wish to use the SendObject method as its too restrictive but this is my only option at the moment.

Should i be capturing error no 287 perhaps?

I do like the code by Carda Consultants - SendHTMLEmail and it works but for the life of me cannot get to to attach a file where I'm passing a variable and have to specify the file other wise i could simply use this. I'm using the 2017-Feb-15 version and their is a later version with better HTML support but the code fails to compile (By VAL Argument Type Mismatch) :cry:.:-

 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 02:50
Joined
Oct 29, 2018
Messages
21,357
Hi,

Yes, i understand what you are saying, if False then it should simply open outlook and send the message without even presenting the email message..:unsure: To be fair i don't really wish to use the SendObject method as its too restrictive but this is my only option at the moment.

Should i be capturing error no 287 perhaps?

I do like the code by Carda Consultants - SendHTMLEmail and it works but for the life of me cannot get to to attach a file where I'm passing a variable and have to specify the file other wise i could simply use this. I'm using the 2017-Feb-15 version and their is a later version with better HTML support but the code fails to compile (By VAL Argument Type Mismatch) :cry:.:-

Just curious, have you tried using True instead of False? I mean, if you're being asked to Allow the email anyway...
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:50
Joined
Sep 21, 2011
Messages
14,038
Hi,

Yes, i understand what you are saying, if False then it should simply open outlook and send the message without even presenting the email message..:unsure: To be fair i don't really wish to use the SendObject method as its too restrictive but this is my only option at the moment.

Should i be capturing error no 287 perhaps?

I do like the code by Carda Consultants - SendHTMLEmail and it works but for the life of me cannot get to to attach a file where I'm passing a variable and have to specify the file other wise i could simply use this. I'm using the 2017-Feb-15 version and their is a later version with better HTML support but the code fails to compile (By VAL Argument Type Mismatch) :cry:.:-

The attachments is meant to be an array?
 

Jason Lee Hayes

Active member
Local time
Today, 09:50
Joined
Jul 25, 2020
Messages
174
Just curious, have you tried using True instead of False? I mean, if you're being asked to Allow the email anyway...
Hi, Yes - I changed False to True just to check and it does the save; still offers the email edit function
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:50
Joined
Oct 29, 2018
Messages
21,357
Hi, Yes - I changed False to True just to check and it does the save; still offers the email edit function
Hi. Thanks for checking. I know it should present the email for edit. What I was trying to see is what happens if you "cancel" the email?

Just to be clear, when you use True, instead of False, you don't get the "Allow" button anymore, right?
 

isladogs

MVP / VIP
Local time
Today, 09:50
Joined
Jan 14, 2017
Messages
18,186
I've not read all of this thread so apologies if this has already been covered.
Try

Code:
If Err = 2501 Then
    ' Report was cancelled, we can ignore this error
    Exit Sub
Else
    MsgBox ...
End If

OR
Code:
If Err <> 2501 Then
    MsgBox ...
End If
 

Jason Lee Hayes

Active member
Local time
Today, 09:50
Joined
Jul 25, 2020
Messages
174
Just an update...

I think I've finally identified what the issue was... Sure, some slight coding oversight which i will take on the chin and learn from highlighted by members of this very helpful group. Don't know if anyone here has run into problems with the Outlook Programmatic Access Settings but looking into this I identified the registry entries which govern this were corrupt (Wrong DWORD values or missing entries!):confused: I believe this is due to a recent uninstall and re-install of Bitdefender prior to continue working on this access dBase which didn't go as expected:cry:, therefore i will mark this thread as resolved as the changes mentioned have been considered & implemented with the Dbase migrated onto a different PC/Environment and works as expected... Thanks for taking the time out to assist - really appreciated (y)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 09:50
Joined
Sep 12, 2006
Messages
15,613
I think that's a windows message, so it's not a trappable access error.

You can also use something like vbMapi from EverythingAccess, which avoids that error.
 

Users who are viewing this thread

Top Bottom