Automatically email sections of a table to specific email addresses

japskib

New Member
Joined
May 3, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I work for a manufacturing company and we receive a daily report which looks something like this:

1588537927222.png


I would like to automatically email each recipient their confirmed stock BUT only their data - no one else's. Ideally the email they receive would look something like this:

"Hi Pets To Go,

Please see below confirmed stock:

1588538258397.png


Kind Regards,

James"


I have some VERY basic knowledge of VBA (we're talking a couple of weeks dabbling) but I'm eager to learn. Any help will be very much appreciated.

Many Thanks,

James
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi

Try this. I haven't used autofilter much so you might be able to tidy it up in due course.
The entire operation is based on the table you posted so any differences you'll need to adjust for.

I modified some code I posted here:
However it doesn't work when there are blank cells in the table so I have changed the code so that it does.

I have used Sheet4 as my table sheet - you'll might want to change that.


The macro rough process:
If it finds worksheet named MailBody it is deleted. (
Adds a new MailBody worksheet
Copies the header and blank second row of the table to MailBody
Filters the rows if they do not have 'yes' in column D
Copies them to A3 of MailBody sheet
Autofits the Maibody sheet content
Creates the email
Uses RangetoHTML function to copy MailBody sheet content to the email with formatting
Adds 'yes' to all rows filtered in column D so it doesn't repeat send
Displays mail for sending
Cleans MailBody sheet - leaves header
Cleans column D of 'yes' when all mails sent
Deletes MailBody sheet

Code:
Sub Send_Mail_autofilter()

Dim MailBody As Range
Dim dwn As Range

  ActiveSheet.AutoFilterMode = False

Set mWs = Worksheets("Sheet4") '<--------------correct sheet?

'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 address rows to
Sheets.Add(After:=Sheets(Sheets.Count)).Name = "MailBody"

'Copy the header and the second blank row as border format will be needed for row 2
mWs.Rows(1).Copy Destination:=Worksheets("MailBody").Range("A1")
  mWs.Rows(2).Copy Destination:=Worksheets("MailBody").Range("A2")

'Return to the mail content sheet
   mWs.Activate

'Set email address as range for first loop to run down
Set rng = Range(Range("C2"), Range("C" & Rows.Count).End(xlUp))

lastRow = ActiveSheet.Range("C" & Rows.Count).End(xlUp).Row
Range("A2:C" & lastRow).Select

  i = rng.Rows.Count

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

Range("A1:C8").Select
Selection.AutoFilter Field:=3, Criteria1:=cell.Value

With ActiveSheet.AutoFilter.Range.Offset(1, 0)

.Copy Sheets("MailBody").Range("A3")
End With

'Need to add yes to each autofiltered row
For Each dwn In rng.SpecialCells(xlCellTypeVisible)
rng.Offset(0, 1).Value = "yes"
Next

'Turn off autofilter
ActiveSheet.AutoFilterMode = False

'Mail header parameters
MailTo = cell.Value 'column E
MailSubject = "Subject?" '<------------------------Subject
   

'Autofit the copied rows on the new sheet
    With Worksheets("MailBody")

    lRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set MailBody = .Range(.Cells(1, 1), .Cells(lRow, 3))
    .Range("A1:C2").Columns.AutoFit

  End With


'Add mail intro
MsgStr = "Hi " & cell.Offset(0, -2).Value _
& "<br><br> Please see below confirmed stock:"

'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, 1).Value = "yes"

'Clear the MailBody rows up to the header
With Worksheets("MailBody")
.Range("A2:C" & .Cells(.Rows.Count, "A").End(xlUp).Row).ClearContents
End With

End If
End If


MailTo = ""
MailSubject = ""
Next

'Clear 'yes' from column D
Range("D2:D" & 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
 
Upvote 0
Hi Deverunt,

Amazing, thank you ever so much for that - huge help. I certainly would not have been able to do that (or even come close) without you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top