I found this great script that works. However, I want to be able to filter and email all the people on a contact list based off the filtered results from the vendor column. The column will have multiple lines that could have multiple vendors who supply a particular part.
I want to be able to modify the script I have to be able to go through and filter and copy all the lines related to that vendor and only send to that particular vendor. It will loop until it has filtered and sent to all the vendors on that sheet.
Example: Vendor TOM has about 10 different items on the spreadsheet. Filter only items for Vendor TOM, copy those items like the chart below, and paste it into an Outlook email in HTML format to send out. It will grab from the Contact list. Then, goes to the next Vendor and filters all items for that Vendor only and repeats the email process until all have been sent.
Currently, the contact list is on another sheet in the same workbook, but I can move the contact list to the same starting in Column Z or something like that.
Does this make sense?
I hope this makes sense. I am very sleepy.
Contact List
I want to be able to modify the script I have to be able to go through and filter and copy all the lines related to that vendor and only send to that particular vendor. It will loop until it has filtered and sent to all the vendors on that sheet.
Example: Vendor TOM has about 10 different items on the spreadsheet. Filter only items for Vendor TOM, copy those items like the chart below, and paste it into an Outlook email in HTML format to send out. It will grab from the Contact list. Then, goes to the next Vendor and filters all items for that Vendor only and repeats the email process until all have been sent.
Currently, the contact list is on another sheet in the same workbook, but I can move the contact list to the same starting in Column Z or something like that.
Does this make sense?
I hope this makes sense. I am very sleepy.
Part | DESCRIPTION | VENDOR | CURRENCY | ORDER QTY | UNIT PRICE |
ab123 | Gidget | VENDOR | CAD | 500 | |
dc4356 | Widget | Select Vendor | USD | 500 | |
Contact List
VENDOR | address | Contact Name | |
VBA Code:
Sub Mail_Sheet_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = Sheets("Summary").Range("A14:L19")
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Range("N5").Value
.CC = ""
.BCC = ""
.Subject = Range("N7").Value
.HTMLBody = RangetoHTML(rng)
.Display 'or use .Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
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