atditiljazi
New Member
- Joined
- Nov 22, 2022
- Messages
- 41
- Office Version
- 365
- Platform
- Windows
hi, i am hoping someone could help me adjusting a macro i found or provide a new one?
i need to send multiple emails with multiple rows as an attachment the the relevant addresses. i ideally would like the email addresses to be in a separate sheet and it would be linked to the supplier codes in the main sheet. the program i export the file doesn't supply the email address so linking the supplier code with the email address is the only way of doing it, i beli
table below
table below of example.
here is the macro i found which needs adjusting and i just don't have no clue where to start because to be honest I don't have a clue about VBA. help will be very appreciated!!!
Sub CreateEmails()
Dim OutApp As Object, OutMail As Object, v As Variant, i As Long, rng As Range
v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
Set OutApp = CreateObject("Outlook.Application")
With CreateObject("scripting.dictionary")
For i = LBound(v) To UBound(v) 'loops through rows
If Not .exists(v(i, 1)) Then
.Add v(i, 1), Nothing
With ActiveSheet
.Range("A1").AutoFilter 1, v(i, 1)
Set rng = .AutoFilter.Range
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = v(i, 2)
.Subject = "This is a test message"
.HTMLBody = RangetoHTML(rng)
.Display
End With
End With
End If
Next i
ActiveSheet.Range("A1").AutoFilter
End With
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"
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
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
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center xublishsource=", "align=left xublishsource=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
i need to send multiple emails with multiple rows as an attachment the the relevant addresses. i ideally would like the email addresses to be in a separate sheet and it would be linked to the supplier codes in the main sheet. the program i export the file doesn't supply the email address so linking the supplier code with the email address is the only way of doing it, i beli
table below
first name | second name | email address |
a | a | aa@email.com |
b | b | bb@email.com |
week | order date | cost | expedite/de-expedite | description | origin | comments | due date | supplier code | company | |||
1 | 10/01/22 | 77 | tree | uk | 03/03/22 | 35432 | a | |||||
2 | 22/01/22 | 5 | logs | uk |
| 454543 | a | |||||
1 | 10/01/22 | 42 | sofa | uk | 02/04/22 | 654645 | b | |||||
2 | 22/01/22 | 32 | tv | uk | 29/01/22 | 453534 | c | |||||
1 | 10/01/22 | 12 | tv | uk | 02/02/22 | 32432432 | c |
here is the macro i found which needs adjusting and i just don't have no clue where to start because to be honest I don't have a clue about VBA. help will be very appreciated!!!
Sub CreateEmails()
Dim OutApp As Object, OutMail As Object, v As Variant, i As Long, rng As Range
v = Range("A2", Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
Set OutApp = CreateObject("Outlook.Application")
With CreateObject("scripting.dictionary")
For i = LBound(v) To UBound(v) 'loops through rows
If Not .exists(v(i, 1)) Then
.Add v(i, 1), Nothing
With ActiveSheet
.Range("A1").AutoFilter 1, v(i, 1)
Set rng = .AutoFilter.Range
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = v(i, 2)
.Subject = "This is a test message"
.HTMLBody = RangetoHTML(rng)
.Display
End With
End With
End If
Next i
ActiveSheet.Range("A1").AutoFilter
End With
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"
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
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
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center xublishsource=", "align=left xublishsource=")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function