VBA: Embedding a Picture in an Outlook Email

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
21,624
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.
 
Just wanted to check on what version of MS Office did your code work?(I'm using 2010. I've read a few places that 2010 has some issues)
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
As I posted earlier in the thread I'm using 2010.
 
Upvote 0
I am also facing the same issue. Email message is showing perfect in the outlook sent item but it is showing as a place holder in the receipients mailbox for example gmail etc.

This is strange! Any one have any clue how do I get rid of it.
 
Upvote 0
I am having the same issue... Hopefully I'm not too late to this tread.

In my case this is what happens when I send the email to myself: If I view the email on the same machine using the same email client, the path to the image still works to pull the file up from my local drive and it displays fine. When I view the email in another email client or on another machine or devise, the path to the image file is disrupted and I get the outline of the image problem that nikhibha mentioned earlier.

If I manually insert the image in Outlook 2013 (Insert --> Picture --> "select the file") then the image is embedded perfectly and can be viewed anywhere. I just can't figure out how to get the image file hard coded into the HTML body of the email using VBA.

When I review the source code for the email being received in Gmail, it looks like the message being received has the embedded image encoded as Base64. Do you guys think that it would need to be converted to Base64 and added to the VBA code that way? How would that work?
 
Upvote 0
This is the format I am currently using to include the image in the code described above...

Code:
""[img src=""cid:Order_Confirmation.gif""]""

(obviously, replaced <> with [] )
 
Upvote 0
musicarnab

Hard to tell what the problem is without seeing any code.

that1guy

Why all the quotes?

PS Same thing about the code.:)
 
Upvote 0
Ha, I was pulling different strings together and didn't even realize the unnecessary quotes. Here are the important bits of code:

Builds email:
Code:
With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

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

    On Error Resume Next
    With OutMail
        .Display
        .To = Worksheets("Customer Sale").Range("C8").Text
        .CC = ""
        .BCC = ""
        .Subject = "Order Confirmation"
        .Attachments.Add "C:\Users[...blah,blah,blah...]Order_Confirmation.gif"
        .HTMLBody = EmailBody & "[br]" & .HTMLBody
        .Display
    End With

With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

Clip from HTML body string to reference attached image:
Code:
"[img src=cid:Order_Confirmation.gif]"

The issue is this inline image references the attachment which doesn't always translate correctly in HTML email format, unless the recipient is using the same computer (not likely). I am trying to find a way to embed the image into the email in the same way that "Insert --> Picture" does in Outlook 2013. This method actually translates the image into Base64 and integrates it into the email itself, rather than referencing an attachment or outside link.

Any ideas on how to tell Outlook to "Insert --> Picture" or otherwise include the Base64 version of the image into the HTML body?
 
Last edited:
Upvote 0
Could this possibly be gif related?
 
Upvote 0
I just tried it with a JPEG and had the same problem. I'm seeing that the source code for both emails (manually inserted image & VBA generated) includes the Base64 encoding for the image. The image added manually ("Insert --> Picture") shows that the file has been renamed and is included under the following header:

Content-Type: image/gif; name="image001.gif" Content-Description: image001.gif Content-Disposition: inline; filename="image001.gif"; size=227314; creation-date="Mon, 01 Jul 2013 16:03:37 GMT"; modification-date="Mon, 01 Jul 2013 16:03:37 GMT" Content-ID: <image001.gif@01ce7642.678128c0> Content-Transfer-Encoding: base64

The email generated through VBA has almost identical source code, but some slight differences:

Content-Type: image/gif; name="Order_Confirmation.gif" Content-Description: Order_Confirmation.gif Content-Disposition: inline; filename="Order_Confirmation.gif"; size=227314; creation-date="Fri, 14 Jun 2013 20:45:25 GMT"; modification-date="Mon, 01 Jul 2013 16:05:01 GMT" Content-Location: Order_Confirmation.gif Content-Transfer-Encoding: base64

My gut says that the renaming isn't so important, since the Base64 translation of the image is present in both emails. The big difference seems to be in the "Content-ID" vs "Content-Location" label for the image. (underlined) However, I don't know enough about HTML email to make too much sense of it, or think of a clever way to fix it.

...hmmm...
</image001.gif@01ce7642.678128c0>
 
Upvote 0
I was having the same issue, trying to get an image into the body, but my solution, as simple as it is, seemed to work for me.
Each time an email was created, the new email took focus, on the "To" field. I simply used the Send Key function to tab down to the body then use the Right click Send Key, and then Paste as image with Send key as well. worked great. Here is the code that was used for that.
Code:
     SendKeys "{TAB}", True
     SendKeys "{TAB}", True
     SendKeys "{TAB}", True
     SendKeys ("+{F10}") 'Rigght Click
     SendKeys "U" 'Paste Image shortcut
 
Upvote 0

Forum statistics

Threads
1,216,146
Messages
6,129,134
Members
449,488
Latest member
qh017

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