SendObject error 2501

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Basenji3
    New Member
    • Mar 2014
    • 14

    SendObject error 2501

    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.

    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
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    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:.

    Comment

    • Basenji3
      New Member
      • Mar 2014
      • 14

      #3
      Originally posted by Seth Schrock
      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:.
      Made the changes as you suggested, but it still will not resume the process.

      Comment

      • Seth Schrock
        Recognized Expert Specialist
        • Dec 2010
        • 2965

        #4
        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.

        Comment

        • Basenji3
          New Member
          • Mar 2014
          • 14

          #5
          Seth,

          It's stopping at the DoCmd.SendObjec t on line 19. When I press F8, it does loop and runs the next record.

          Comment

          • Seth Schrock
            Recognized Expert Specialist
            • Dec 2010
            • 2965

            #6
            When you cancel an email, do you get the "eMail not sent" message from line 34?

            Comment

            • zmbd
              Recognized Expert Moderator Expert
              • Mar 2012
              • 5501

              #7
              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):
              Code:
              Option Compare Database
              Option Explicit
              
              Private Sub Command12_Click()
              'Dim Variables here
              '
              'Set the error traping
                  On Error GoTo zerrortrap
              '
              'set objects and variables as needed
              '
              'action:
                  DoCmd.SendObject _
                      TO:="alpha.zulu@mailinator.com", _
                      Subject:="test message", _
                      MessageText:="testing email traps - close/cancel this email to see if 2501 traps", _
                      editmessage:=True
              '
              error_Exit:
              'Clean up after code is ran or if there was an error
              '
                  Exit Sub
              '
              zerrortrap:
                  Select Case Err.Number
                      Case 2501
                          MsgBox "User Canceled the Email.", vbInformation
                      Case Else
                          MsgBox "Error " & Err.Number & " " & Err.Description
                      End Select
                  Resume error_Exit
              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.
              Last edited by zmbd; Mar 13 '14, 07:37 PM.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32656

                #8
                When you execute :
                Code:
                Resume ExitHandler
                on line #35 you have come out of the error handling logic. The following line (#30) :
                Code:
                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.

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  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 (^_^)

                  Comment

                  • Basenji3
                    New Member
                    • Mar 2014
                    • 14

                    #10
                    Originally posted by zmbd
                    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 (^_^)
                    Okay, I'll keep playing with it, even with your test code, it still continues to stop at the docmd.sendobjec t line, it never reaches the error handler.

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

                    Thanks.

                    Comment

                    • zmbd
                      Recognized Expert Moderator Expert
                      • Mar 2012
                      • 5501

                      #11
                      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.

                      Comment

                      Working...