We help IT Professionals succeed at work.

The sendobject action was cancelled

iainmacleod
iainmacleod asked
on
I am using a button to send an email and have the problem when closing the email rather than sending.
I cannot seem to figure out other code examples found so hoped that by showing my modest code might persuade someone to offer me a little assistance.
Thanks in advance.
Private Sub emailcertcmd_Click()

Dim dbs As Database, customers As Recordset, certemail As Variant, stDocName As String
Set dbs = CurrentDb
stDocName = "Main Certificateemail"
Set cust = dbs.OpenRecordset("customers", dbOpenDynaset)
On Error Resume Next


cust.MoveFirst
    Do Until cust.EOF
        If Me.Accno = cust![Account No] Then
            certemail = cust![certemail]
        End If
            cust.MoveNext
    Loop
   
DoCmd.SendObject acSendReport, "Main Certificateemail", acFormatPDF, certemail, , , "Your Freightnet (RASCO) Ltd REST Certificate is attached", "Please find your Certificate attached", True
DoCmd.RunMacro ("main printcert")
db.Close
Set db = Nothing
End Sub
Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2013

Commented:
Try using error handling to trap that specific error (I think it is error 2501):

Private Sub emailcertcmd_Click()

Dim dbs As Database, customers As Recordset, certemail As Variant, stDocName As String
Set dbs = CurrentDb
stDocName = "Main Certificateemail"
Set cust = dbs.OpenRecordset("customers", dbOpenDynaset)
On Error Goto EH   '<---- Change this to send errors to an error handler


cust.MoveFirst
    Do Until cust.EOF
        If Me.Accno = cust![Account No] Then
            certemail = cust![certemail]
        End If
            cust.MoveNext
    Loop
   
DoCmd.SendObject acSendReport, "Main Certificateemail", acFormatPDF, certemail, , , "Your Freightnet (RASCO) Ltd REST Certificate is attached", "Please find your Certificate attached", True
DoCmd.RunMacro ("main printcert")
db.Close
Set db = Nothing
Exit sub  '<---- Exit if no errors

EH:   '<--- error handler
    if err.number = 2501 then
           msgbox "The email was not sent"
           resume next
   else if
          msgbox Err.number & ": " & err.description
          resume next
   end if

End Sub
Top Expert 2016

Commented:


cust.MoveFirst
    Do Until cust.EOF
        If Me.Accno = cust![Account No] Then
            certemail = cust![certemail]

         exit do   '<< ADD this , to end the search once you found the email


        End If
            cust.MoveNext
    Loop
   

Author

Commented:
Thanks for the swift reply.
The error handler works perfectly on my workstation running full Access (I do get an error telling me an object is required after the "Email not sent" message, however I have just realised that once i put this in a runtime version i still get the Send object error priot to the "email not sent" msg.

Most Valuable Expert 2012
Top Expert 2013
Commented:
I'm not sure why you are getting the error message in the Runtime version.

Your object required message is easily fixed, though. Your object names are inconsistent:

Take a look at your abbreviated code below:

Dim dbs As Database, customers As Recordset, certemail As Variant, stDocName As String
Set dbs = CurrentDb
          ' The rest of your code here...
db.Close
Set db = Nothing
End Sub

You are setting "dbs" for your database object, but closing "db". They all should be either dbs or db (take your pick).

It is also a good idea to include the line "Option Explicit" at the very top of each code module, right under the "Option Compare Database" line. This will trap undeclared variables like this one as compile errors -- so that they do not cause problems at runtime.

Author

Commented:
Thanks very much for your advice, gratefully recieved. I am finding this coding really useful and effective but clearly i lack some of the basics...