Home > Enterprise >  Add image (chart) to HTMLbody
Add image (chart) to HTMLbody

Time:12-08

I am having issues with inserting image (chart) into an HTMLbody. I export the chart to another folder and then call the image path.

msg = "<html>123,<br/> 123 <b>" & countries & ":</b><br/>" & RangetoHTML(tablex) & s & "<img src=""cid:" & fileName & "><html\>"

However, after I insert the image with the above message body it shows:

enter image description here

And after the adjustments by correctly specifying which image, I get:

enter image description here

To specify the exact image I use:

Set myChart = wbe.Sheets("Sheet1").ChartObjects("Chart 11").Chart

Dim myPicture As String
Dim fileName As String
Dim myPath As String

myPicture = "Chart1.jpg"
myPath = "C:\qwe\"

fileName = myPath & myPicture
myChart.Export fileName

Whole code:

Sub transactional_emails()

'Create email and save it as draft
    Dim olApp As Object
    Dim olMailItm As Object
    Dim iCounter As Integer
    Dim Dest As Variant
    Dim SDest As String
    Dim source As String
    Dim oAccount As String
    Dim msg As String
    Dim tablex As Range
    Dim wbe As Workbook
    Dim las As Long
    Dim countries As String
    Dim myChart As Chart
    
    countries = "LOL"
    
    Set wbe = Workbooks(ThisWorkbook.Name)
    
    las = wbe.Sheets("Sheet1").Cells(wbe.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Row
    
    Set tablex = wbe.Sheets("Sheet1").Range("A1:G" & las)
    
    With tablex.Borders
        .LineStyle = xlContinuous
        .Color = vbBlack
        .Weight = xlThin
    End With
    
    
    Set myChart = wbe.Sheets("Sheet1").ChartObjects("Chart 11").Chart
    
    Dim myPicture As String
    Dim fileName As String
    Dim myPath As String

    myPicture = "Chart1.jpg"
    myPath = "C:\qwe\"

    fileName = myPath & myPicture
    myChart.Export fileName
    

    'Create the Outlook application and the empty email.
    Set olApp = CreateObject("Outlook.Application")
    Set olMailItm = olApp.CreateItem(0)
    
    Dim s As String
    s = Environ("appdata") & "\Microsoft\Signatures\"
    If Dir(s, vbDirectory) <> vbNullString Then s = s & Dir$(s & "*.htm") Else s = ""
    s = CreateObject("Scripting.FileSystemObject").GetFile(s).OpenAsTextStream(1, -2).ReadAll
    
    msg = "<html>123,<br/> 123 <b>" & countries & ":</b><br/>" & RangetoHTML(tablex) & s & "<img src=""cid:" & myPicture & """></html>"
    
    With olMailItm
        SDest = "[email protected]"
        'oAccount = "[email protected]"
    
        .To = SDest
        .CC = "[email protected]"
        .Subject = countries & " 123 " & Date
        .Attachments.Add fileName, 1, 0
        .htmlbody = msg
        .Save
    End With
    
    'Clean up the Outlook application.
    Set olMailItm = Nothing
    Set olApp = Nothing

End sub

CodePudding user response:

The source code is valid:

  1. You attach an image to the mail item in Outlook:
.Attachments.Add fileName, 1, 0
  1. Then you can refer to the attached image from the message body in the following way:
"<img src=""cid:" & myPicture & """>
  1. Sometimes you also need to set the PR_ATTACH_CONTENT_ID property (DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F) on the attachment using Attachment.PropertyAccessor.

You may also consider adding some parameters like height or width. Note, the image name can't contains spaces.

When you open an email message that contains images in Microsoft Office Outlook, the image areas can be blocked. Read more about that in the Pictures cannot be displayed and are shown as red X in Outlook article.

  • Related