By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,464 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,464 IT Pros & Developers. It's quick & easy.

SendObject error 2501

P: 14
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.

Expand|Select|Wrap|Line Numbers
  1. On Error GoTo ErrorHandler
  2. Dim rsLoop As Recordset
  3. Dim strSQL As String
  4. Dim db As Database
  5. Dim stDocname As String
  6.                                    '  bombs when I delete an email from being sent (doesn't resume)
  7. stDocname = "5MgrRpt"
  8. DoCmd.SetWarnings False
  9.  
  10. Set db = CurrentDb
  11. Set rsLoop = db.OpenRecordset("Select * FROM tblLoop;")
  12.     rsLoop.MoveFirst
  13.     While Not rsLoop.EOF
  14.         strSQL = "INSERT INTO tblResults (ReportsTo, EmailAddress)"
  15.         strSQL = strSQL & " SELECT  ReportsTo![Reports-To], ReportsTo!EmailAddress FROM ReportsTo"
  16.         strSQL = strSQL & " WHERE ReportsTo![Reports-To] ='" & rsLoop!R2 & "'"
  17.         db.Execute (strSQL), dbFailOnError
  18.  
  19.         DoCmd.SendObject acReport, stDocname, "PdfFormat(*.pdf)", rsLoop!emailAddress, "", "", "Manager Approval", "", True, """"""
  20.         DoCmd.OpenQuery "5Del", acNormal, acEdit
  21.     rsLoop.MoveNext
  22.  
  23.     Wend
  24.     rsLoop.Close
  25.     Set rsLoop = Nothing
  26.     Set db = Nothing
  27.     MsgBox "Reports Completed", vbOKOnly, "Month End PTO"
  28.  
  29. ExitHandler:
  30.     Resume Next
  31. ErrorHandler:
  32.   Select Case Err  'specific Case statements for errors
  33.     Case 2501       'Action SendObject was cancelled.
  34.       MsgBox "eMail not sent"
  35.       Resume ExitHandler
  36.     Case Else
  37.       MsgBox Err.Description
  38.       Resume Command54_Exit
  39.   End Select
  40.  
  41. Command54_Exit:
  42.         Exit Sub
Mar 13 '14 #1
Share this Question
Share on Google+
10 Replies


Seth Schrock
Expert 2.5K+
P: 2,951
Your error handler is errored. Your Select Case should test for Err.Number not just Err. Also, you need to put your Command54_Exit: code above the ExitHandler: code so that if no error occurs, your code will exit before getting to the ErrorHandler:.
Mar 13 '14 #2

P: 14
@Seth Schrock
Made the changes as you suggested, but it still will not resume the process.
Mar 13 '14 #3

Seth Schrock
Expert 2.5K+
P: 2,951
Do you get the "eMail not sent" message box? If yes, when it comes up, press Ctrl + Break. This will make the code enter Break mode and allow you to step through the code. The MsgBox line should be highlighted. Press F8 to step through the code one line at a time. This will tell you what lines are being executed and when. Post back this information and we can go from there.

I don't think that this would stop if from working, but you really don't need the ExitHandler. Just put Resume Next to replace line 35.
Mar 13 '14 #4

P: 14
Seth,

It's stopping at the DoCmd.SendObject on line 19. When I press F8, it does loop and runs the next record.
Mar 13 '14 #5

Seth Schrock
Expert 2.5K+
P: 2,951
When you cancel an email, do you get the "eMail not sent" message from line 34?
Mar 13 '14 #6

zmbd
Expert Mod 5K+
P: 5,397
GOTCHA!
Welcome to the hell of MS.
Sometimes this will trap and other times it wont.

Try this simple code in the "On Click" event:
Insert a new command button.... here it was named Command12 just because this is for example, the importaint part is the code within the SUB()/EndSub.

(Because this was a new form, the following code is what I had in the form's class module):
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub Command12_Click()
  5. 'Dim Variables here
  6. '
  7. 'Set the error traping
  8.     On Error GoTo zerrortrap
  9. '
  10. 'set objects and variables as needed
  11. '
  12. 'action:
  13.     DoCmd.SendObject _
  14.         TO:="alpha.zulu@mailinator.com", _
  15.         Subject:="test message", _
  16.         MessageText:="testing email traps - close/cancel this email to see if 2501 traps", _
  17.         editmessage:=True
  18. '
  19. error_Exit:
  20. 'Clean up after code is ran or if there was an error
  21. '
  22.     Exit Sub
  23. '
  24. zerrortrap:
  25.     Select Case Err.Number
  26.         Case 2501
  27.             MsgBox "User Canceled the Email.", vbInformation
  28.         Case Else
  29.             MsgBox "Error " & Err.Number & " " & Err.Description
  30.         End Select
  31.     Resume error_Exit
  32. End Sub
So on my system this works just fine and the Error 2501 pops the msgbox.
Once you have this simple code working on your form, then we can use it to build your loop.
Mar 13 '14 #7

NeoPa
Expert Mod 15k+
P: 31,768
When you execute :
Expand|Select|Wrap|Line Numbers
  1. Resume ExitHandler
on line #35 you have come out of the error handling logic. The following line (#30) :
Expand|Select|Wrap|Line Numbers
  1. Resume Next
is error handling code. Resume only works within error handling code, which you've just come out of. Hence - it fails. You would want it to, as it makes no sense where it is - either for resumed code or the flow of code dropping into it naturally.
Mar 14 '14 #8

zmbd
Expert Mod 5K+
P: 5,397
Neopa is absolutly correct, and Seth had pointed out some issues too; thus, I considered the OP code to be somewhat, well, unusable, and as I don't have time to re-write code, hence the code I offered to start with so that the initial framework is good, then you can tweek from there.

Sometimes a small victory is all that is needed to get the project off the ground (^_^)
Mar 14 '14 #9

P: 14
@zmbd
Okay, I'll keep playing with it, even with your test code, it still continues to stop at the docmd.sendobject line, it never reaches the error handler.

At least this is a good challenge going forward for me.

Thanks.
Mar 14 '14 #10

zmbd
Expert Mod 5K+
P: 5,397
The code I provided works in ACC2010: in an very mature database I have (backup of my production database), A newer yet used test database, and a brand new database.

Thus, if it's not working in your database, there might be something corrupted or code in one of the modules that has something stalled.

Try opening a brand new database, create only an unbound form, add the action button, add my framework code to the click event, compile and save.

Switch the form to normal view and click the button, see what happens.
Mar 15 '14 #11

Post your reply

Sign in to post your reply or Sign up for a free account.