0% found this document useful (0 votes)
4 views8 pages

Border

The document contains a VBA script for sending emails with an HTML table generated from data in an Excel worksheet. It includes two subroutines, 'Send_Table' and 'Send_Table_2', which handle the creation of the email body and the management of duplicate email addresses. Additionally, it features a function to convert a range to HTML and a function to check if a worksheet exists.

Uploaded by

Mukesh Sain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Border

The document contains a VBA script for sending emails with an HTML table generated from data in an Excel worksheet. It includes two subroutines, 'Send_Table' and 'Send_Table_2', which handle the creation of the email body and the management of duplicate email addresses. Additionally, it features a function to convert a range to HTML and a function to check if a worksheet exists.

Uploaded by

Mukesh Sain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Sub Send_Table()

'Set email address as range for first loop to run down


Set rng = Range(Range("E2"), Range("E" & Rows.Count).End(xlUp))

'Get a row count to clear column H at the end


x = rng.Rows.Count

PgStart = "<html><body>"

'Create the html table and header from the first row
tableHdr = "<table border=1><tr><th>" & Range("A1").Value & "</th>" _
& "<th>" & Range("B1").Value & "</th>" _
& "<th>" & Range("C1").Value & "</th>" _
& "<th>" & Range("D1").Value & "</th>" _
& "<th>" & Range("E1").Value & "</th>" _
& "<th>" & Range("F1").Value & "</th>" _

'Check to see if column G = 'yes' and skip mail if it does


For Each cell In rng
If cell.Value <> "" Then
If Not cell.Offset(0, 2).Value = "yes" Then

NmeRow = cell.Row

Set OutApp = CreateObject("Outlook.Application")


Set OutMail = OutApp.createitem(0)

MailTo = cell.Value 'column E


mailcc = cell.Offset(0, -3).Value
MailSubject = "Subject?"

'Create MailBody table row for first row


MailBody = "<tr>" _
& "<td>" & cell.Offset(0, -4).Value & "</td>" _
& "<td>" & cell.Offset(0, -3).Value & "</td>" _
& "<td>" & cell.Offset(0, -2).Value & "</td>" _
& "<td>" & cell.Offset(0, -1).Value & "</td>" _
& "<td>" & cell.Value & "</td>" _
& "<td>" & cell.Offset(0, 1).Value & "</td>" _
& "</tr>"

'Second loop checks the email addresses of all cells following the current cell in the first
loop.
'Yes will be appended on any duplicate finds and another row added to the mailbody table
For Each dwn In rng.Offset(NmeRow - 1, 0)

If dwn.Value = cell.Value Then

'Create additional table row for each extra row found


AddRow = "<tr>" _
& "<td>" & dwn.Offset(0, -4).Value & "</td>" _
& "<td>" & dwn.Offset(0, -3).Value & "</td>" _
& "<td>" & dwn.Offset(0, -2).Value & "</td>" _
& "<td>" & dwn.Offset(0, -1).Value & "</td>" _
& "<td>" & dwn.Value & "</td>" _
& "<td>" & dwn.Offset(0, 1).Value & "</td>" _
& "</tr>"

dwn.Offset(0, 2).Value = "yes"


MailBody = MailBody & AddRow 'column A

End If
' Clear additional table row variable ready for next
AddRow = ""
Next

MsgStr = "<p>Dear " & cell.Offset(0, 1).Value & "<br><br>" _


& "Please see below</p><br>"

With OutMail
.To = MailTo
.CC=mailcc
.Subject = MailSubject
.HTMLBody = PgStart & MsgStr & tableHdr & MailBody & "</table></body></html>"
.Display
'send
End With

cell.Offset(0, 2).Value = "yes"

End If
End If

MailTo = ""
MailSubject = ""
MailBody = ""
Next

'Clear 'yes' from all appended cells in column H


Range("G2:G" & x + 1).ClearContents
End Sub
Sub Send_Table_2()

Dim MailBody As Range

Set mWs = Worksheets("Sheet1")

'If MailBody sheet already exists then delete it


If WorksheetExists("MailBody") Then

Application.DisplayAlerts = False

Worksheets("MailBody").Delete

Application.DisplayAlerts = True

End If

'Add a sheet to copy all same person rows to

Sheets.Add(After:=Sheets(Sheets.Count)).Name = "MailBody"

'Copy the header

mWs.Rows(1).Copy Destination:=Worksheets("MailBody").Range("A1")

'Return to the mail content sheet

mWs.Activate

'Set email address as range for first loop to run down

Set rng = Range(Range("E2"), Range("E" & Rows.Count).End(xlUp))

'Get a row count to clear column H at the end

i = rng.Rows.Count

For Each cell In rng

If cell.Value <> "" Then

If Not cell.Offset(0, 2).Value = "yes" Then

'Get the row number

cRow = cell.Row
'Copy the row to the first empty row in the MailBody sheet

mWs.Range("A" & cRow, "F" & cRow).Copy Destination:=Sheets("MailBody").Cells(Rows.Count,


"A").End(xlUp).Offset(1, 0)

MailTo = cell.Value 'column E

mailcc = cell.Offset(0, -3).Value

MailSubject = "Subject?"

'Second loop checks the email addresses of all cells following the current cell in the first loop.

'Yes will be appended on any duplicate finds and another row added to the MailBody sheet

For Each dwn In rng.Offset(cRow - 1, 0)

If dwn.Value = cell.Value Then

dwn.Offset(0, 2).Value = "yes"

'Create additional table row for each extra row found

mWs.Range("A" & cRow, "F" & cRow).Copy Destination:=Sheets("MailBody").Cells(Rows.Count,


"A").End(xlUp).Offset(1, 0)

End If

Next

'Autofit the copied rows on the new sheet, otherwise they'll be copied to the mail with defaults widths,
heights

With Worksheets("MailBody")

lRow = .Range("A" & .Rows.Count).End(xlUp).Row

Set MailBody = .Range(.Cells(1, 1), .Cells(lRow, 6))


.Range("A1:F2").Columns.AutoFit

End With

'Add mail intro

MsgStr = "Dear " & cell.Offset(0, 1).Value _

& "<br><br> Please see below"

'Create mail

Set OutApp = CreateObject("Outlook.Application")

Set OutMail = OutApp.createitem(0)

With OutMail

.To = MailTo

.CC = mailcc

.Subject = MailSubject

.HTMLBody = MsgStr & RangetoHTML(MailBody)

.Display

'send

End With

cell.Offset(0, 2).Value = "yes"

'Clear the MailBody rows up to the header

With Worksheets("MailBody")

.Range("A2:F" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents

End With

End If

End If
MailTo = ""

MailSubject = ""

Next

'Clear 'yes' from column G

Range("G2:G" & i + 1).ClearContents

'Delete MailBody sheet

Application.DisplayAlerts = False

Worksheets("MailBody").Delete

Application.DisplayAlerts = True

End Sub

Function RangetoHTML(rng As Range) ' Changed by Ron de Bruin 28-Oct-2006

' Working in Office 2000-2016

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 -4163, , False, False

.Cells(1).PasteSpecial -4122, , 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:=4, _

filename:=TempFile, _

Sheet:=TempWB.Sheets(1).Name, _

Source:=TempWB.Sheets(1).UsedRange.Address, _

HtmlType:=0)

.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

'Does the worksheet exists

Function WorksheetExists(WSName) As Boolean

On Error Resume Next

WorksheetExists = Worksheets(WSName).Name = WSName

On Error GoTo 0

End Function

You might also like