I want to use an array of email addresses and for each address in the array attach a different file.
The following code works okay the first time through, but the second time through it attaches both files and inserts the first two emails in the array.
I think I need something after the IF statement to clear everything for the second loop through, but I don't know what to use to clear.
Code:
Private Sub cmdSendIt_Click()
'send outlook email .zip attachments.
Dim OlApp As Object
Dim OlMail As Object
Dim ToRecipient As Variant
Dim CcRecipient As Variant
Dim i As Integer
Dim x As Variant
Set OlApp = CreateObject("Outlook.Application")
Set OlMail = OlApp.createitem(olmailitem)
ToRecipient = Array("Jon.Doe@gmail.com", "Jane.Doe@yahoo.com", "Mary.Smith@hotmail.com")
For i = 0 To 1
x = ToRecipient(i)
'add email address from array
OlMail.Recipients.Add x
'fill in Subject field
OlMail.Subject = "TEST Auto email with attachment
'body test>
OlMail.Body = "This is to test the automatic sending of reports."
If i = 0 Then
'Add the .zip file as an attachments
OlMail.Attachments.Add "C:\Test\01.zip"
ElseIf i = 1 Then
OlMail.Attachments.Add "C:\Test\02.zip"
Else: MsgBox "Check it"
End If
'Display the message
OlMail.Display 'change this to OlMail.Send if you just want to send it without previewing it
' clear everything here......I think.
Next i
End Sub