I am running the code below, but I am having problems with it looping to the whole list of email address. It only generates one email, and it is being sent to the 1st email add. What should I change?
Code:
Option Explicit
Sub DistroPDF()
Application.ScreenUpdating = False
With Application
If .Calculation <> xlCalculationAutomatic Then
.Calculation = xlCalculationAutomatic
End If
End With
ThisWorkbook.Activate
'Declares variables used on the code
Dim outApp As Object
Dim outMail As Object
Dim distro As Worksheet
Dim setup As Worksheet
Dim sendTo, subj, attachment, msg, ccTo, sender As String
Dim status, stamp As String
Dim line1, line2, line3, line4, line5, line6, line7 As String
Dim lastRow As Long
Dim rng As Range
Dim cell As Range
Dim s As String
s = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(s, vbDirectory) <> vbNullString Then s = s & Dir$(s & "Henkel Personal.htm") Else s = ""
s = CreateObject("Scripting.FileSystemObject").GetFile(s).OpenAsTextStream(1, -2).ReadAll
'defines the sheets assigned to the variables
Set setup = ThisWorkbook.Sheets("SetUp")
Set distro = ThisWorkbook.Sheets("Distro")
distro.Activate
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Nothing
'counts the numbers of rows and is the number of emails going to be generated
Set rng = Range("C2:C" & lastRow)
'creates email
Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(0)
'error catchers
On Error GoTo cleanup
For Each cell In rng 'loops within the list
sender = setup.Range("B2").Value2
sendTo = Range(cell.Address).Offset(0, 0).Value2 'defines where the emails are sent
subj = setup.Range("B1").Value2 'defines the subject (subject can be modified on sheet Setup)
attachment = "\" & Range(cell.Address).Offset(0, 2).Value2 'defines the attachments per email
'ccTo = Range(cell.Address).Offset(0, 1).Value2 'defines anyone copied on the email
msg = Range(cell.Address).Offset(0, -2).Value2
line1 = setup.Range("B4")
line2 = setup.Range("B5")
line3 = setup.Range("B6")
line4 = setup.Range("B7")
line5 = setup.Range("B8")
line6 = setup.Range("B9")
line7 = setup.Range("B10")
On Error Resume Next
With outMail 'actual email creation
.SentOnBehalfofName = sender
.To = sendTo
'.CC = ccTo
.Subject = subj
.Attachments.Add (ThisWorkbook.Path & attachment)
'"<p style='font-family:Segoe UI;font-size:14'>" &
.HTMLBody = "Dear " & msg & "," & "<br><br>"
.HTMLBody = .HTMLBody & line1 & "<br><br>"
.HTMLBody = .HTMLBody & line2 & "<br>"
.HTMLBody = .HTMLBody & line3 & "<br><br>"
.HTMLBody = .HTMLBody & line4 & "<br><br>"
.HTMLBody = .HTMLBody & line5
'.HTMLBody = .HTMLBody & line6
'.HTMLBody = .HTMLBody & line7
.HTMLBody = .HTMLBody & s
.ReadReceiptRequested = True
.Send 'this code will send the email.
'If you want to see the email first before it is sent out,
'use .Display instead
End With
status = Range(cell.Address).Offset(0, 3).Select
ActiveCell.FormulaR1C1 = "Sent" 'Status of the email
stamp = Range(cell.Address).Offset(0, 4).Select
ActiveCell.FormulaR1C1 = "=NOW()" 'timestamp for when the email is sent
On Error Resume Next
Set outMail = Nothing
Next cell 'loop
cleanup:
Set outApp = Nothing
With Application
.DisplayAlerts = False
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub