6

I would like to send a Excel chart in the body of an email (Outlook) (not as attachment) from VB, anyone know how to do this?

Solved:
Just to add a bit more detail to answer below you'll need the following (could do with some improvement).

Sheets(2).ChartObjects(1).Chart.Export "C:\temp\Chart2.png"

....

   .HTMLBody = "<html xmlns:o='urn:schemas-microsoft-com:office:office'" & _
            "xmlns: x = 'urn:schemas-microsoft-com:office:excel'" & _
            "xmlns='http://www.w3.org/TR/REC-html40'> " & _
            "<head></head><body><img src='Chart2.png'></body></html>"

and

    .Attachments.Add ("C:\temp\Chart2.png")

2 Answers 2

5

Seems like the best way is to export the chart:

Sheets(1).ChartObjects("Chart 1").Chart.Export "C:\Chart1.png"

And then add the image into your mail HTML body:

.HTMLBody = .HTMLBody & "< img src='c:\folder\filename.png'>"

Confirmed by both technet and mrexcel

3
  • Thanks spent about 2 days looking for a solution.
    – aronp
    Commented Apr 24, 2012 at 8:00
  • @JMax. This worked like a charm when I tried sending the image to my own outlook address, but when I tried forwarding the same mail with the embedded image to my colleagues, they were unable to see the image. The same thing happened when I tried forwarding it to my gmail account. Any idea what could cause this?
    – vestland
    Commented Aug 15, 2016 at 10:10
  • @123apd when forwarding an email in Outlook, you have to manually confirm you want to download the image (try to do it by hand first and then automatize)
    – JMax
    Commented Aug 17, 2016 at 8:00
5

I thought I'd add my solution here in addition to the one above since it is using both a sub and a temporary file path, in case you are interested.

I changed the code from here to insert a chart using a temporary path (they do the same thing using a range of cells in the workbook):

Sub createGraph(nameSheet As String, nameChart As String, nameFile As String)
  ThisWorkbook.Activate
  Worksheets(nameSheet).Activate
  With ThisWorkbook.Worksheets(nameSheet).ChartObjects(nameChart)
    .Activate
    .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
  End With
End Sub

In the HTML body put:

Call createGraph("Graphs", "Chart 1", "filename")
TempFilePath = Environ$("temp") & "\"
.Attachments.Add TempFilePath & "filename.jpg", olByValue, 0
.HTMLBody = .HTMLBody & "<img src='cid:filename.jpg'>"

By using "olByValue, 0" the attachment is added at position 0 and not visible in the email as an attachment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.