CookieMonster76
Board Regular
- Joined
- Apr 30, 2015
- Messages
- 201
Hi
I've put together the below to send an e-mail containing a table from Excel to multiple e-mail addresses that are in a list in excel. It works for the 1st address in the list but fails at the point highlighted bold when it comes to the second line.
Is there a way to correct it so it will send however many e-mails I want it to?
Thanks
Paul
Sub SendEmails()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim i As Integer
Dim lastRow As Long
Dim emailCol As Integer
Dim conditionCol As Integer
Dim xInspect As Object
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
' Define your columns
emailCol = 1 ' Assuming email addresses are in column A
AddresseeCol = 2
ValueCol = 3
MonthCol = 4
conditionCol = 5 ' Assuming your condition (Yes/No) is in column B
' Get the last row with data in column A
lastRow = Cells(Rows.Count, emailCol).End(xlUp).Row
' Create Outlook instance
Set OutlookApp = CreateObject("Outlook.Application")
' Loop through each row
For i = 2 To lastRow ' Assuming the first row is headers
If Cells(i, conditionCol).Value = "Yes" Then
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.To = Cells(i, emailCol).Value
.Subject = "West Midlands Pension Fund"
.Body = "Hi " & Cells(i, AddresseeCol).Value & vbLf & vbLf _
& "You owe us " & Cells(i, ValueCol).Value & vbLf & vbLf _
& "in relation to your " & Cells(i, MonthCol).Value & " submission" & vbLf & vbLf _
& "Please let me know if you have any queries." & vbLf & vbLf _
& "Thanks" & vbLf & vbLf _
& "Bev" & vbLf & vbLf
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheets("Table").Range("A1:G9").Copy
pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteSpecial (wdFormatPlainText)
.Display
.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
Set OutlookMail = Nothing
End If
Next i
Set OutlookApp = Nothing
MsgBox "Emails sent successfully!"
End Sub
I've put together the below to send an e-mail containing a table from Excel to multiple e-mail addresses that are in a list in excel. It works for the 1st address in the list but fails at the point highlighted bold when it comes to the second line.
Is there a way to correct it so it will send however many e-mails I want it to?
Thanks
Paul
Sub SendEmails()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim i As Integer
Dim lastRow As Long
Dim emailCol As Integer
Dim conditionCol As Integer
Dim xInspect As Object
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
' Define your columns
emailCol = 1 ' Assuming email addresses are in column A
AddresseeCol = 2
ValueCol = 3
MonthCol = 4
conditionCol = 5 ' Assuming your condition (Yes/No) is in column B
' Get the last row with data in column A
lastRow = Cells(Rows.Count, emailCol).End(xlUp).Row
' Create Outlook instance
Set OutlookApp = CreateObject("Outlook.Application")
' Loop through each row
For i = 2 To lastRow ' Assuming the first row is headers
If Cells(i, conditionCol).Value = "Yes" Then
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.To = Cells(i, emailCol).Value
.Subject = "West Midlands Pension Fund"
.Body = "Hi " & Cells(i, AddresseeCol).Value & vbLf & vbLf _
& "You owe us " & Cells(i, ValueCol).Value & vbLf & vbLf _
& "in relation to your " & Cells(i, MonthCol).Value & " submission" & vbLf & vbLf _
& "Please let me know if you have any queries." & vbLf & vbLf _
& "Thanks" & vbLf & vbLf _
& "Bev" & vbLf & vbLf
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheets("Table").Range("A1:G9").Copy
pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteSpecial (wdFormatPlainText)
.Display
.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
Set OutlookMail = Nothing
End If
Next i
Set OutlookApp = Nothing
MsgBox "Emails sent successfully!"
End Sub