Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed picture in outlook mail body excel vba

Tags:

excel

vba

outlook

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
like image 328
Stupid_Intern Avatar asked Jul 02 '17 10:07

Stupid_Intern


People also ask

How do I insert a picture into the body of an Outlook email?

Insert a picture into the body of an email message Position your cursor where you want the image in your message. In the ribbon, select Insert > Pictures. Browse your computer or online file locations for the picture you want to insert. Select the picture, then select Insert.

How do I display an image in Excel VBA?

Press Alt + F11 to start the Visual Basic Editor (VBE). Press with left mouse button on "Insert" on the menu, see image above. Press with left mouse button on "Module". Paste VBA code to window.


1 Answers

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

like image 76
Siddharth Rout Avatar answered Oct 16 '22 09:10

Siddharth Rout