atditiljazi
New Member
- Joined
- Nov 22, 2022
- Messages
- 41
- Office Version
- 365
- Platform
- Windows
hello,
i currently use a macro to send multiple emails and i want it tweaked so it sends it as an attachment, not in the body of the email. i cant seem to amend it. can anyone help? my macro is below
Sub create_multiple_emails()
Dim sh As Worksheet
Dim c As Range, rng As Range
Dim dic As Object
Dim lr As Long
Set sh = Sheets("order book")
Set dic = CreateObject("scripting.dictionary")
If sh.AutoFilterMode Then sh.AutoFilterMode = False
lr = sh.Range("g" & Rows.Count).End(xlUp).Row
For Each c In sh.Range("g2:g" & lr)
If Not dic.exists(c.Value) Then
dic(c.Value) = dic(c.Value)
sh.Range("A1:z" & lr).AutoFilter 7, c.Value
Set rng = sh.Range("A1:H" & lr)
With CreateObject("Outlook.Application").CreateItem(0)
.To = c.Value
.Subject = "Subject"
.HTMLBody = RangetoHTML(rng)
'.Send 'to send
.display 'to show
End With
End If
Next
sh.ShowAllData
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object, ts As Object
Dim TempFile As String, TempWB As Workbook
TempFile = Environ$("temp") & "\temp.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(xlSourceRange, TempFile, TempWB.Sheets(1).Name, TempWB.Sheets(1).UsedRange.Address, 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 xublishsource=", "align=left xublishsource=")
'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
i currently use a macro to send multiple emails and i want it tweaked so it sends it as an attachment, not in the body of the email. i cant seem to amend it. can anyone help? my macro is below
Sub create_multiple_emails()
Dim sh As Worksheet
Dim c As Range, rng As Range
Dim dic As Object
Dim lr As Long
Set sh = Sheets("order book")
Set dic = CreateObject("scripting.dictionary")
If sh.AutoFilterMode Then sh.AutoFilterMode = False
lr = sh.Range("g" & Rows.Count).End(xlUp).Row
For Each c In sh.Range("g2:g" & lr)
If Not dic.exists(c.Value) Then
dic(c.Value) = dic(c.Value)
sh.Range("A1:z" & lr).AutoFilter 7, c.Value
Set rng = sh.Range("A1:H" & lr)
With CreateObject("Outlook.Application").CreateItem(0)
.To = c.Value
.Subject = "Subject"
.HTMLBody = RangetoHTML(rng)
'.Send 'to send
.display 'to show
End With
End If
Next
sh.ShowAllData
End Sub
Function RangetoHTML(rng As Range)
Dim fso As Object, ts As Object
Dim TempFile As String, TempWB As Workbook
TempFile = Environ$("temp") & "\temp.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(xlSourceRange, TempFile, TempWB.Sheets(1).Name, TempWB.Sheets(1).UsedRange.Address, 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 xublishsource=", "align=left xublishsource=")
'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