Results 1 to 10 of 10
  1. #1
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544

    Send PDF's to individual recipients

    Good morning all! I am attempting to email monthly statements to members. I can successfully export them to separate PDF files, but when it comes to actually sending those PDF files to the appropriate member, I am getting the error: "The item has been moved or deleted". It errors on the .To=strto line. Can anyone tell me what I've got wrong please? TIA!! Hope you're having a great day!

    Code:
    Private Sub Command100_Click()
    Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
    Dim fileName As String, todayDate As String
    Dim rst As DAO.Recordset
    Dim LastName As Variant
    Dim strBody As String, lngCount As Long, lngRSCount As Long
    Dim strTo As String
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM [emailinvoicelist]ORDER BY [lastname];", dbOpenSnapshot)
    Do While Not rst.EOF
        strRptFilter = "[memberid_PK] = " & rst![MemberID_PK]
        DoCmd.OutputTo acOutputReport, "GroupAsmtStatement", acFormatPDF, "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf"
        DoEvents
        rst.MoveNext
    Loop
    'DoCmd.OutputTo acReport, "emailGroupAsmtStatement2", acFormatPDF, fileName
    MsgBox "file created"
    'Email the results of the report generated
    Set oEmail = oApp.CreateItem(olMailItem)
    With oEmail
          rst.MoveLast
          rst.MoveFirst
          Do Until rst.EOF
          lngCount = lngCount + 1
          
          strTo = rst!InvoiceEmail
          fileName = "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf"
         
         '.Recipients.Add rst!InvoiceEmail
        Debug.Print rst!InvoiceEmail
        .TO = strTo
        .Subject = "Statement Attached"
        .Body = "Thank you"
        .Attachments.Add fileName
        
            .Send
        rst.MoveNext
          Loop
        
    End With
    MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
    End Sub
    


  2. #2
    Wayne is offline Novice
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Location
    Toronto, Canada
    Posts
    6
    See in red below. Is that a command line or a comment line? You started it with an apostrophe.

    Wayne

    Code:
    Private Sub Command100_Click()
    Dim oApp As New Outlook.Application Dim oEmail As Outlook.MailItem Dim fileName As String, todayDate As String Dim rst As DAO.Recordset Dim LastName As Variant Dim strBody As String, lngCount As Long, lngRSCount As Long Dim strTo As String Set rst = CurrentDb.OpenRecordset("SELECT * FROM [emailinvoicelist]ORDER BY [lastname];", dbOpenSnapshot) Do While Not rst.EOF strRptFilter = "[memberid_PK] = " & rst![MemberID_PK] DoCmd.OutputTo acOutputReport, "GroupAsmtStatement", acFormatPDF, "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf" DoEvents rst.MoveNext Loop 'DoCmd.OutputTo acReport, "emailGroupAsmtStatement2", acFormatPDF, fileName MsgBox "file created" 'Email the results of the report generated Set oEmail = oApp.CreateItem(olMailItem) With oEmail rst.MoveLast rst.MoveFirst Do Until rst.EOF lngCount = lngCount + 1 strTo = rst!InvoiceEmail fileName = "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf" '.Recipients.Add rst!InvoiceEmail Debug.Print rst!InvoiceEmail .TO = strTo .Subject = "Statement Attached" .Body = "Thank you" .Attachments.Add fileName .Send rst.MoveNext Loop End With MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS" End Sub

  3. #3
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Hi Wayne, it's actually commented out because I already have
    Code:
    DoCmd.OutputTo acOutputReport, "GroupAsmtStatement", acFormatPDF, "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf"
    above it. I should have erased it, but, since it references a different report, I left it in for my own information.

    Thanks!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Does it send the first one (my guess is yes)? I think you need to create the email inside the loop, not before.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    it doesn't send any, it error right away (after it outputs the pdf's). I will try it inside the loop. Thanks Paul!

  6. #6
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Not getting the error access error -2147418107 it is illegal to call out while inside message filter. Errors on line "
    Set oEmail = oApp.CreateItem(olMailItem)", I have never encountered this error before and I thought I'd seen them all!
    Code:
    Private Sub Command100_Click()
    Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
    Dim fileName As String, todayDate As String
    Dim rst As DAO.Recordset
    Dim LastName As Variant
    Dim strBody As String, lngCount As Long, lngRSCount As Long
    Dim strTo As String
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM [emailinvoicelist]ORDER BY [lastname];", dbOpenSnapshot)
    Do While Not rst.EOF
        strRptFilter = "[memberid_PK] = " & rst![MemberID_PK]
        DoCmd.OutputTo acOutputReport, "emailGroupAsmtStatement2", acFormatPDF, "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf"
        DoEvents
        rst.MoveNext
    
          
    Set oEmail = oApp.CreateItem(olMailItem)
    With oEmail
         
          
          strTo = rst!InvoiceEmail
          fileName = "C:\Clients\EmailTESTS" & "\" & rst![MemberID_PK] & ".pdf"
         
         '.Recipients.Add rst!InvoiceEmail
        Debug.Print rst!InvoiceEmail
        .TO = strTo
        Debug.Print strTo
        Debug.Print fileName
        .Subject = "Statement Attached"
        .Body = "Thank you"
        .Attachments.Add fileName
        .Send
        rst.MoveNext
      
        
    End With
    Loop
    MsgBox "Email successfully sent!", vbInformation, "EMAIL STATUS"
    End Sub
    

  7. #7
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    Ok, so this is weird, I copied the code to add it here, then continued and it "skipped" the error and actually emailed the statements! What the heck? It has self corrected, just ran it successfully two times. Had something to do with an Excel file I had open (no idea why, but, I closed it and it ran!) Thank you all!!

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You've got 2 MoveNext lines in that unless I'm going blind. This is the relevant part from a working process:

    Code:
        Do While Not rs.EOF
            Forms!frmStatementFilter.txtEmailCust = rs!CustomerAccount
    
            DoCmd.OutputTo acOutputReport, strReport, acFormatPDF, "c:\AccessAp\" & rs!CustomerAccount & "Statement.pdf"
    
            Set MyOutlook = CreateObject("Outlook.Application")
            Set MyMail = MyOutlook.CreateItem(0)
    
            strEmail = rs!Email
            MyMail.To = strEmail 
            MyMail.Subject = rs!CompanyName & " statement"
    
            Set myattachments = MyMail.Attachments
            myattachments.Add "c:\AccessAp\" & rs!CustomerAccount & "Statement.pdf"
    
            MyMail.Body = "Your statement is attached"
            MyMail.display
            'MyMail.send
    
            rs.MoveNext
        Loop
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Gina Maylone is offline Always learning
    Windows 10 Access 2016
    Join Date
    Jun 2013
    Location
    Afton, MN
    Posts
    544
    No, you're not going blind, you're absolutely correct! I took out the first one and NOW I'm getting 3 emails instead of 2, so YAY!!! THANKS AGAIN!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 11-27-2015, 04:45 PM
  2. Replies: 4
    Last Post: 06-26-2015, 10:31 AM
  3. Send email to multiple recipients based on query
    By nablmm in forum Programming
    Replies: 3
    Last Post: 09-11-2014, 05:36 PM
  4. Replies: 6
    Last Post: 03-19-2014, 03:53 PM
  5. Replies: 2
    Last Post: 12-07-2011, 07:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums