Image in email VBA

himynameis

New Member
Joined
Nov 9, 2022
Messages
1
Office Version
  1. 2019
Platform
  1. 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 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
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

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