Option Explicit
Sub sendmail()
Dim OutApp As Object
Dim OutMail As Object
Dim SigString As String
Dim Signature, EmailTo, CCto, Subj, msg, Filepath As String
Dim ws As Worksheet
Dim cel As Range
Dim LR As Long
Dim rng As Range
Set ws = Sheets("MRM")
‘The next line requires editing for the Sheet and Range of data to copy.
Set rng = Sheets("Sheet1").Range("B4:J10")
With ws
LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
If Not .AutoFilterMode Then
.Range("A3:P3").AutoFilter
End If
.Range("A3:P" & LR).AutoFilter Field:=16, Criteria1:="<>"
If .Range("P3:P" & LR).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
For Each cel In .Range("P4:P" & LR).SpecialCells(xlCellTypeVisible)
EmailTo = .Cells(cel.Row, "J").Value
CCto = .Cells(cel.Row, "K").Value
Subj = .Cells(cel.Row, "L").Value
Filepath = .Cells(cel.Row, "M").Value
msg = .Cells(cel.Row, "N").Value
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
SigString = Environ("appdata") & _
"\Microsoft\Signatures\mrm.htm"
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
On Error Resume Next
With OutMail
.To = EmailTo
.CC = CCto
.BCC = ""
.Subject = Subj
‘Use the following line to include range of data in email body
.HTMLBody = msg & "<br><br>" & "Please review the following : " & RangetoHTML(rng)
'.body = msg & vbNewLine & vbNewLine & Signature
' .Attachments.Add Filepath 'Uncomment this Line if you've added attachments
.Display '.Send 'or use .Display
End With
Next cel
End If
.AutoFilterMode = False
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Function GetBoiler(ByVal sFile As String) As String
'**** Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.ReadAll
ts.Close
End Function
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