12

I am trying to embed a range from a worksheet as an image in outlook mail body. It's saving the picture correctly but I only see blank image in the outlook mail body. What am I doing wrong here?

Sub View_Email()

    tName = Trim(MAIN.Range("tEmail"))

    If Not tName Like "*@*.*" Then MsgBox "Invalid Email address": Exit Sub

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'File path/name of the gif file
    Fname = ThisWorkbook.Path & "\Claims.jpg"

    Set oCht = Charts.Add

    STAT.Range("A3:G26").CopyPicture xlScreen, xlBitmap
    With oCht
        .Paste
        .Export Filename:=Fname, Filtername:="JPG"
        '.Delete
    End With

    On Error Resume Next
    With OutMail
        .To = tName
        .CC = ""
        .BCC = ""
        .Subject = STAT.Range("C1").Value
        .HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
                    "<img src=" & Fname & "' height=520 width=750>"
        .display
        '.Send   'or use .Display
    End With
    On Error GoTo 0

    'Delete the gif file
    'Kill Fname

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
Stupid_Intern
  • 3,382
  • 8
  • 37
  • 74

2 Answers2

28

You need to add the image and hide it. The position 0 will add and hide it.

.Attachments.Add Fname, 1, 0

The 1 is the Outlook Constant olByValue

Once you add the image then you have to use "cid:FILENAME.jpg" as shown below.

Try this

With OutMail
    .To = tName
    .CC = ""
    .BCC = ""
    .Subject = STAT.Range("C1").Value
    .Attachments.Add Fname, 1, 0
    .HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
                "<img src=""cid:Claims.jpg""height=520 width=750>"
    .Display
End With

Screenshot

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 1
    Adding this incase anyone was having the same trouble I was, remember in HTML you can't have spaces in your file name :) my pictures were not showing up until I changed my "File Name" to "File_Name". – hammythepig Dec 17 '18 at 20:25
  • 2
    @hammythepig Actually, you can but you just need to [encode](https://www.w3schools.com/TAGS/ref_urlencode.asp) your URL to handle spaces in filenames or paths. So to make `File Name` valid, it should be encoded as `File%20Name`. – L42 Oct 11 '19 at 09:57
  • For the life of me I can't get this to work - the quotes in the middle (" – Selkie Oct 29 '19 at 21:19
  • @Selkie: What problem are you facing? – Siddharth Rout Oct 29 '19 at 23:21
  • 1
    @SiddharthRout: I get an "image can't be displayed, link might be broken" - I'm trying to dynamically bring in the filename (Temp_Picture_1.JPG), but I've also tried just the string - I just can't seem to hit on the right "" needed, and copy-pasting the sample results in the compiler complaining about " - tried a chr(34) replacement, but still no go. I'm actually re-writing all of the code at home to make a post now.... – Selkie Oct 30 '19 at 00:22
  • @SiddharthRout: https://stackoverflow.com/questions/58617458/how-to-embed-an-image-into-an-outlook-email-using-vba – Selkie Oct 30 '19 at 01:09
  • This is a good solution. In addition to that, what needs to be done if the received mail is expected to appear without attachement.. i.e. I want to send mail with embeded image but should not appear as a mail with attachment for its recipient. – Jaqen H'ghar Nov 09 '20 at 14:09
  • @JaqenH'ghar: In that case you have to sort of embed the image in the email html by first storing the image on a public server and then inserting that link in the html. – Siddharth Rout Nov 09 '20 at 16:44
0

You need to set the PR_ATTACH_CONTENT_ID MAPI property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F") using Attachment.PropertyAccessor.SetProperty and refer that attachment through the src attribute that matches the value of PR_ATTACH_CONTENT_ID set on the attachment. PR_ATTACH_CONTENT_ID corresponds to the Content-ID MIME header when the message is sent.

attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
MailItem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"

Keep in mind that setting content-id is preferable to using the attachment file name in the <img> element since it would need to be properly encoded and (if I remember correctly) some e-mail clients have a problem with just using the attachment file name for images.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78