How to email embedded image from excel

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

It is decided:
Just add a little more detail to answer below. you will need the following (perhaps 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") 
+6
source share
2 answers

It seems the best way is to export the chart :

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

And then add the image to your HTML postal body :

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

Confirmed by both technet and mrexcel

+5
source

I thought I would add my solution here in addition to the one mentioned above, since it uses both the auxiliary and temporary file paths if you are interested.

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

 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 put tag:

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

Using "olByValue, 0", the attachment is added to position 0 and is not displayed as an attachment in the message.

+3
source

Source: https://habr.com/ru/post/913930/


All Articles