himynameis
New Member
- Joined
- Nov 9, 2022
- Messages
- 1
- Office Version
- 2019
- Platform
- Windows
I'm trying to attach multiple images (9) into an email sandwiched be text.
The text all comes through but the 2 sets of 3 images (Slot 1-3 & TG 1-3) after the first set don't populate in the email.
the code I'm using is below.
Thanks for any help!!
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Dim Rng4 As Range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set rng = Sheets("Body").Range("A14:Z20").SpecialCells(xlCellTypeVisible)
Set rng2 = Sheets("Body").Range("A40:Z48").SpecialCells(xlCellTypeVisible)
Set rng3 = Sheets("Body").Range("A68:Z77").SpecialCells(xlCellTypeVisible)
Set Rng4 = Sheets("Body").Range("A97:AC102").SpecialCells(xlCellTypeVisible)
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.htmlbody = RangetoHTML(rng) & _
"<img src='C:\Export\GGR_1.png'>" & _
"<img src='C:\Export\GGR_2.png'>" & _
"<img src='C:\Export\GGR_3.png'>" & _
"<BR>" & _
RangetoHTML(rng2) & _
"<BR>" & _
"<img src='C:\Export\Slot_1.png'>" & _
"<img src='C:\Export\Slot_2.png'>" & _
"<img src='C:\Export\Slot_3.png'>" & _
"<BR>" & _
RangetoHTML(rng3) & _
"<BR>" & _
"<img src='C:\Export\TG_1.png'>" & _
"<img src='C:\Export\TG_2.png'>" & _
"<img src='C:\Export\TG_3.png'>" & _
RangetoHTML(Rng4) & _
"<BR>" & _
.htmlbody
.Display
End With
On Error GoTo 0
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 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
The text all comes through but the 2 sets of 3 images (Slot 1-3 & TG 1-3) after the first set don't populate in the email.
the code I'm using is below.
Thanks for any help!!
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Dim Rng4 As Range
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set rng = Sheets("Body").Range("A14:Z20").SpecialCells(xlCellTypeVisible)
Set rng2 = Sheets("Body").Range("A40:Z48").SpecialCells(xlCellTypeVisible)
Set rng3 = Sheets("Body").Range("A68:Z77").SpecialCells(xlCellTypeVisible)
Set Rng4 = Sheets("Body").Range("A97:AC102").SpecialCells(xlCellTypeVisible)
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.htmlbody = RangetoHTML(rng) & _
"<img src='C:\Export\GGR_1.png'>" & _
"<img src='C:\Export\GGR_2.png'>" & _
"<img src='C:\Export\GGR_3.png'>" & _
"<BR>" & _
RangetoHTML(rng2) & _
"<BR>" & _
"<img src='C:\Export\Slot_1.png'>" & _
"<img src='C:\Export\Slot_2.png'>" & _
"<img src='C:\Export\Slot_3.png'>" & _
"<BR>" & _
RangetoHTML(rng3) & _
"<BR>" & _
"<img src='C:\Export\TG_1.png'>" & _
"<img src='C:\Export\TG_2.png'>" & _
"<img src='C:\Export\TG_3.png'>" & _
RangetoHTML(Rng4) & _
"<BR>" & _
.htmlbody
.Display
End With
On Error GoTo 0
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 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