Option Explicit
Sub EmailSend()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim eList As Range: Set eList = ws.Range("B2:B" & ws.Cells(ws.Rows.Count, 2).End(xlUp).Row)
Dim hdr As Range: Set hdr = ws.Range("A1:F1")
Dim c As Range, iRow As Range
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem:
For Each c In eList.Cells
'Emails require adding the "Microsoft Outlook 16.0 Object Library" reference
'This can be added in Tools > References
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = ws.Cells(c.Row, 7).Value
.CC = ws.Cells(c.Row, 8).Value
.Subject = ""
.HTMLBody = "Dear " & ws.Cells(c.Row, 2) & "," & "<br>" & "<br>" & "Thanks for doing Business," & _
"<br>" & "<br>" & RangetoHTML(hdr) & RangetoHTML(ws.Range(ws.Cells(c.Row, 1), ws.Cells(c.Row, 6))) & _
"<br>" & "<br>" & "Thanks and Regards,"
.Display
End With
Set oMail = Nothing
Set oApp = Nothing
Next c
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function