Hi everyone
I'm sending emails to customers selected from a listbox (Arnelgp was so kind to help me with this one )
I have two reports to create for each customer to be attached to the individual email. Each set of reports has data specific to each customer.
I've added the report creating lines as in the code below but it's not working. It creates the first set of reports for the first record in the listbox and uses the same reports for all the other records.
How do I handle this scenario?
Thank you in advance!!
I'm sending emails to customers selected from a listbox (Arnelgp was so kind to help me with this one )
I have two reports to create for each customer to be attached to the individual email. Each set of reports has data specific to each customer.
I've added the report creating lines as in the code below but it's not working. It creates the first set of reports for the first record in the listbox and uses the same reports for all the other records.
How do I handle this scenario?
Thank you in advance!!
Code:
Private Sub Cmd_EmailSaleGuar_Click()
Dim strbody As String
Dim chkGuaranteeSent As String
Dim OutMail As Outlook.MailItem
Dim OutApp As New Outlook.Application
Dim i As Long
If Forms!frmContactGroupListbox!ContactList.ItemsSelected.Count < 1 Then
Exit Sub
End If
Set OutApp = CreateObject("Outlook.Application")
For i = 0 To Forms!frmContactGroupListbox!ContactList.ListCount - 1
If Forms!frmContactGroupListbox!ContactList.Selected(i) Then
strbody = "Dear " & Forms!frmContactGroupListbox!ContactList.Column(2, i) & "<br><br>" _
& "Attached are the Sale Agreement and Health Guarantee for the puppy who will soon be part of your family. Attached as well, are copies of the Vaccination <br> " _
& "Certificate, Health Check and Change of Ownership form.<br> <br> " _
& "Both the Sale Agreement and Change of Ownership forms need to be completed, signed and returned to us, even if you choose to submit the form yourself. <br> <br>" _
& "Kind Regards <br><br><br>" _
& "REDROBIENA HOUSE OF PAWS"
'Create the Reports
DoCmd.OpenReport "rptSaleGuarantee", acViewPreview, , "DogID=" & Forms!frmContactGroupListbox!ContactList.Column(0, i), acWindowNormal
DoCmd.OutputTo acOutputReport, "rptSaleGuarantee", acFormatPDF, "c:\Emails\rptSaleGuarantee" & "_" & Forms!frmContactGroupListbox!ContactList.Column(3, i) & ".pdf", False
DoCmd.OpenReport "rptChangeOwnership", acViewPreview, , "DogID=" & Forms!frmContactGroupListbox!ContactList.Column(0, i), acWindowNormal
DoCmd.OutputTo acOutputReport, "rptChangeOwnership", acFormatPDF, "c:\EMails\rptChangeOwnership" & "_" & Forms!frmContactGroupListbox!ContactList.Column(3, i) & ".pdf", False
Set OutMail = OutApp.CreateItem(0)
With OutMail
'create the email
.To = Forms!frmContactGroupListbox!ContactList.Column(4, i)
.Subject = "Sale Guarantee & Health Declaration"
.HTMLBody = strbody
'add the reports we created
.attachments.Add "c:\Emails\rptSaleGuarantee" & "_" & Forms!frmContactGroupListbox!ContactList.Column(3, i) & ".pdf"
.attachments.Add "c:\EMails\rptChangeOwnership" & "_" & Forms!frmContactGroupListbox!ContactList.Column(3, i) & ".pdf"
'send the email
.Display
End With
End If
Next
MsgBox "Sale Guarantee & Change of Ownership reports can be retrieved from C:\Emails", vbInformation
'tidy up..
Set OutMail = Nothing
Set OutApp = Nothing