I have a statement that loops through a table to send a report via email. If I send all the emails it works fine, but if I cancel and email, I get the run-time error 2501 Can't sendobject.
I have an error handler in place, but I can't get the code to resume.
I have an error handler in place, but I can't get the code to resume.
Code:
On Error GoTo ErrorHandler
Dim rsLoop As Recordset
Dim strSQL As String
Dim db As Database
Dim stDocname As String
' bombs when I delete an email from being sent (doesn't resume)
stDocname = "5MgrRpt"
DoCmd.SetWarnings False
Set db = CurrentDb
Set rsLoop = db.OpenRecordset("Select * FROM tblLoop;")
rsLoop.MoveFirst
While Not rsLoop.EOF
strSQL = "INSERT INTO tblResults (ReportsTo, EmailAddress)"
strSQL = strSQL & " SELECT ReportsTo![Reports-To], ReportsTo!EmailAddress FROM ReportsTo"
strSQL = strSQL & " WHERE ReportsTo![Reports-To] ='" & rsLoop!R2 & "'"
db.Execute (strSQL), dbFailOnError
DoCmd.SendObject acReport, stDocname, "PdfFormat(*.pdf)", rsLoop!emailAddress, "", "", "Manager Approval", "", True, """"""
DoCmd.OpenQuery "5Del", acNormal, acEdit
rsLoop.MoveNext
Wend
rsLoop.Close
Set rsLoop = Nothing
Set db = Nothing
MsgBox "Reports Completed", vbOKOnly, "Month End PTO"
ExitHandler:
Resume Next
ErrorHandler:
Select Case Err 'specific Case statements for errors
Case 2501 'Action SendObject was cancelled.
MsgBox "eMail not sent"
Resume ExitHandler
Case Else
MsgBox Err.Description
Resume Command54_Exit
End Select
Command54_Exit:
Exit Sub
Comment