VBA: Embedding a Picture in an Outlook Email

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
21,612
Office Version
  1. 365
Platform
  1. Windows
Is it at all possible to embed a picture in an email? I've tried...

Code:
.HtmlBody = " < img src=""C:/Users/Domenic/Desktop/Sample.png"" > "

However, while the picture gets inserted in the email, it's not actually embedded. As a result, when the recipient receives the email, the image is not displayed.
 
Norie,

Do you mean that the picture would be sent with the email as an attachment and that the picture would then be displayed in the email itself? If so, how would you do it?
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Domenic

This is code from 2004 but it works for me with Outlook 2010 sending the message.

However when I receive the message in Outlook the image is on a different 'page', but when I receive it at my Google Mail account it's fine.

That could be down to some setting I've got in Outlook, or just a problem I'm having - it wouldn't be the first trouble I've had with images in email.

Actually just read something that says the problem is Outlook 2010, something to do with inline CSS not being supported any more.

I knew about that but not that it would cause this sort of problem.

Anyway, enough wittering here's the code:

Rich (BB code):
Option Explicit

'Add reference to MS Outlook x.x Object Library
'Picture to be added as an attachment and modified src location for each embedded picture.
Private Sub Command1_Click()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
    'create new Outlook MailItem
    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(olMailItem)
    'add graphic as attachment to Outlook message
    'change path to graphic as needed
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add("C:\Users\Norie\Pictures\FlyingPuffin.jpg")
    oEmail.Close olSave
    'change the src property to 'cid:your picture filename'
    'it will be changed to the correct cid when its sent.
    oEmail.HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
                      "<IMG alt='' hspace=0 src='cid:FlyingPuffin.jpg' align=baseline border=0> </BODY>"
    oEmail.Save
    oEmail.Display    'fill in the To, Subject, and Send. Or program it in.
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing
End Sub
 
Upvote 0
Thanks Norie! I'm logging off right now, but I'll take a look at it as soon as I get a chance and let you know. Thanks again!
 
Upvote 0
Norie,

It works beautifully. Thank you very much for your help. Much appreciated.

sanits591,

Thank you for your help as well. Much appreciated.
 
Upvote 0
No problem.

I take it you aren't using Outlook 2010.:)
 
Upvote 0
Well it didn't work properly for me.

When I receive the message the picture hasn't been embedded in body of the email.

I had to click on the attachment to see it.

Must be some setting I suppose.:)
 
Upvote 0
I just tried it again, just to be sure, and it seemed to work properly. I didn't need to click on the attachment. I started the email with an embedded image, and then I followed it with some text. In testing it, I sent the email to myself. If you'd like, I can email the sample to you so you can see for yourself.
 
Last edited:
Upvote 0
Thanks Domenic.

Worked fine, and I found the problem - the HTML in the code I tested with was wroing.:)
 
Upvote 0

Forum statistics

Threads
1,215,706
Messages
6,126,344
Members
449,311
Latest member
accessbob

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top