ThrottleWorks
Excel Ninja
Hi,
I am creating an e-mail with VBA.
E-mail body contains some text lines and a chart.
I am able to create e-mail body with text however I am not able to paste chart in e-mail.
Can anyone please help in this. Please see below code for your reference.
Set EmailRng = EmaiSht.Range("F1:Q" & TempLr) is e-mail body.
EmailSht is a worksheet in my macro.
I am supposed to paste a chart below this range. Chart is in another file.
When I use ChartSht.ChartObjects("Chart 1").Copy and paste in EmaiSht.
Blank chart window is pasted instead of proper chart.
I am creating an e-mail with VBA.
E-mail body contains some text lines and a chart.
I am able to create e-mail body with text however I am not able to paste chart in e-mail.
Can anyone please help in this. Please see below code for your reference.
Set EmailRng = EmaiSht.Range("F1:Q" & TempLr) is e-mail body.
EmailSht is a worksheet in my macro.
I am supposed to paste a chart below this range. Chart is in another file.
When I use ChartSht.ChartObjects("Chart 1").Copy and paste in EmaiSht.
Blank chart window is pasted instead of proper chart.
Code:
Option Explicit
Sub Create_Email()
Dim EmailRng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim TempLr As Long
TempLr = EmaiSht.Cells(EmaiSht.Rows.Count, 6).End(xlUp).Row
Set EmailRng = EmaiSht.Range("F1:Q" & TempLr)
ChartSht.ChartObjects("Chart 1").Copy
TempLr = EmaiSht.Cells(EmaiSht.Rows.Count, 6).End(xlUp).Row + 2
EmaiSht.Cells(TempLr, 6).PasteSpecial xlPasteAll
MacroBook.Activate
EmaiSht.Select
EmaiSht.Range("A1").Select
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim MyPath As String
MyPath = ThisWorkbook.Worksheets("Mapping").Range("A2").Value
Dim Signature As String
Dim olsave As Variant
With OutMail
.display
Signature = OutMail.HTMLBody
.To = EmailMapSht.Range("B1").Value
.CC = EmailMapSht.Range("B2").Value
.Subject = EmailMapSht.Range("B3").Value
.Attachments.Add (MapSht.Range("E10").Value)
.Attachments.Add (MapSht.Range("E12").Value)
.HTMLBody = RangetoHTML(EmailRng) & Signature
.display
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
Dim MacroBook As Workbook
Dim EmailSht As Worksheet
Set MacroBook = ThisWorkbook
Set EmailSht = MacroBook.Worksheets("EmailSht")
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
Dim TempLr As Long
Dim EmailRng As Range
TempLr = EmailSht.Cells(EmailSht.Rows.Count, 6).End(xlUp).Row
Set EmailRng = EmailSht.Range("F1:Q100")
EmailRng.Copy
Set TempWB = Workbooks.Add(1)
TempWB.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
TempWB.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteFormats
TempLr = Cells(Rows.Count, 1).End(xlUp).Row
Dim TempSht As Worksheet
Set TempSht = TempWB.Worksheets("Sheet1")
Cells.Font.Name = "Calibri"
Cells.Font.Size = 10
With TempWB.Sheets(1)
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 x:publishsource=", "align=left x:publishsource=")
RangetoHTML = Replace(RangetoHTML, "<!--[if !excel]> <![endif]-->", "")
TempWB.Close savechanges:=False
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Last edited: