I need to send an email with a range of cells from a workbook in the body of the email, and also a different attachent for each recipient, in Excel 2007.
I am having difficulty with the code below. Everything works as intended except for adding the attachments. When I start the loop to send the emails with their respective attachments, it includes all the previous iterations' attachments. That is to say the emails send like this:
Email 1 - Attachment 1
Email 2 - Attachment 1, Attachment 2
Email 3 - Attachment 1, Attachment 2, Attachment 3; and so on.
Sub Send_Range()
Dim x As Integer
Dim i As Integer
x = Sheets("MarketMacro").Range("M1").Text 'A count of how many emails to send.
i = 2
Do
' Select the range of cells on the active worksheet.
Sheets("Summary").Range("A1:M77").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Introduction = "This is a sample worksheet."
.Item.To = Sheets("MarketMacro").Range("A" & i).Text
.Item.Subject = "Test" 'email subject
.Item.attachments.Add (Sheets("MarketMacro").Range("H" & i).Text) 'add attachment based on path in worksheet cell
.Item.Send 'sends without displaying the email
End With
i = i + 1
Loop Until i = x + 2
MsgBox ("The tool sent " & i - 2 & " reports.")
End Sub
Does anyone have a solution to this problem? I have another way to send the emails programmatically with attachments that works perfectly fine, but I am unable to send a range of cells as the body the email.