Share via

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

Anonymous
2012-12-10T01:21:33+00:00

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"

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-12-10T01:41:11+00:00

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?

Was this answer helpful?

5 people found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-05-01T20:51:39+00:00

    The error handler works for me as well.  But I have a new problem.  If I try to execute the Click() again, Access locks up on me and I have to kill it with TaskManager.  It always works on the first Click(), but fails with the lockup on the 2nd.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-12-11T03:05:55+00:00

    Thank you Doug for the advice, I will rename it. I'm currently experimenting, trying to get a db up and running and still trying to get up to speed with vba.

    Just a quick question, what does Resume Cleanup do?

    Was this answer helpful?

    0 comments No comments