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