Hello
I am attempting to copy one active sheet into html body outlook message.
I have the following code, which is adding the attachments however my charts still do not appear.
I am attempting to add one chart for now, but once complete will need to add all 5 from the page.
Any help with the code would be much, much appreciated.
Thank you
Sub Mail_Sheet_Outlook_Body1650()
Dim Rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strto As String
Dim Fname1 As String
Dim FnameRoot As String
Const ForReading As Long = 1
Const olMailItem = 0
Const olFormatHTML = 2
Const UseDefault As Long = -2
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
FnameRoot = "C:\Users\kroberts\desktop"
Fname1 = FnameRoot & "chart1.gif"
With ActiveWorkbook.Worksheets("EMAILSHEET").ChartObjects("Chart 1")
.Activate
.Chart.Export Filename:=Fname1, FilterName:="GIF"
End With
Set Rng = Sheets("EMAILSHEET").UsedRange
For Each cell In ThisWorkbook.Sheets("ADDRESSES").Range("A3:A10")
If cell.Value Like "?*@?*.?*" Then
strto = strto & cell.Value & ";"
End If
Next cell
If Len(strto) > 0 Then strto = Left(strto, Len(strto) - 1)
On Error Resume Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.attachments.Add Fname1
.Save
.To = strto
.CC = ""
.BCC = ""
.Subject = "MARKET UPDATE 1650"
.BodyFormat = olFormatHTML
.HTMLBody = RangetoHTML(Rng) & "<IMG src=cid:desktopchart1.gif></img>"
.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
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
Bookmarks