Error message Run-time error '2501': the SendObject action was cancelled.

Hi

 

I have the following vba.

 

Private Sub Command25_Click()

Me.Dirty = False

DoCmd.OpenReport "nonconformity", acViewPreview, , "[nonconformid] =" & Me.NonConformID

DoCmd.SendObject acSendReport, , acFormatPDF, Forms![non conformity]![Contact]

DoCmd.Close acReport, "NonConformity", acSaveNo

End Sub

 

This sends a report based on the current record displayed on the form to an email as a pdf attachment.

After sending the email, it closes the report and the user is back to the form .

This all works fine as long as the email is sent.

 I'm trying to make this fullproof should a user decide not to send the email and close it instead, the following error displays when email is cancelled.

Error message Run-time error '2501': the SendObject action was cancelled

 

Can someone help me with the code to do the following: if email is cancelled, complete the vba (also closing the open report) & close the report displaying "Email message was Cancelled"

Else, "Message Sent Successfully"

 

 

 

Question Info


Last updated May 21, 2019 Views 2,930 Applies to:
Answer
Answer

You need to include error trapping in your procedure:

Private Sub Command25_Click()
On Error GoTo ErrorHandler

  Me.Dirty = False
  DoCmd.OpenReport "nonconformity", acViewPreview, , "[nonconformid] =" & Me.NonConformID

  DoCmd.SendObject acSendReport, , acFormatPDF, Forms![non conformity]![Contact]
  DoCmd.Close acReport, "NonConformity", acSaveNo
  MsgBox "Message Sent Successfully."

Cleanup:
  Exit Sub

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

End Sub

Incidentally, do yourself a huge favour, and give your controls meaningful names. You may know today that Command25 is the command button that sends the email, but will you when you need to go back to your application a week or a month from now?

Doug Steele, Microsoft Access MVP
www.AccessMVP.com/djsteele

2 people were helped by this reply

·

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.