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.
 

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)
Hi Norie

This is the code I tried to use but I am still getting the red cross where this image is supposed to be. I've got Outlook 2010. Any idea why its happening?(I've added spaces before and after "<" & ">" to allow the code to show):
Code:
Sub mailTest()
'Add reference to MS Outlook x.x Object Library
'Picture to be added as an attachment and modified src location for each embedded picture.




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("D:\TAT\TAT_Summary.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 = " < IMG src=cid:'TAT_Summary.jpg' > "
    
    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
The name of the picture should not be in quotes.
 
Upvote 0
I removed the single quotes and tried it. Now I can see an outline of where the image should be and in it a line, "Description cid:TAT_Summary.jpg"

Also added the following lines
Code:
oEmail.To="someAddress@something.com"
oEmail.Subject="Something"
oEmail.Send

I removed the save and display lines. Now when the mail goes, the image isnt even present in the body. Could you explain what's happening and how to resolve it? I am fairly new to VBA.
Thanks
 
Upvote 0
Try this for the HTMLBody.
HTML:
   oEmail.HTMLBody = "<IMG src=cid:TAT_Summary.jpg>"
 
Upvote 0
This is my code as of now. Now the image has disappeared from the body altogether on receiving. However the image is present in the body of my sent mail(In sent mail folder). Added a space before and after "<" and ">".

Rich (BB code):
Sub mailTest()
'Add reference to MS Outlook x.x Object Library
'Picture to be added as an attachment and modified src location for each embedded picture.
Dim oApp As Outlook.Application
Dim oEmail As MailItemDim 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("D:\TAT\TAT_Summary.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 = " < IMG src=cid:TAT_Summary.jpg > " 
oEmail.To="someone@something.com"    
oEmail.Subject="something"    
oEmail.Send    

Set oEmail = Nothing    
Set colAttach = Nothing    
Set oAttach = Nothing    
Set oApp = Nothing
End Sub
 
Last edited:
Upvote 0
Is it possible that the picture is being blocked/stripped for some reason?
 
Upvote 0
I tried your code, with a different picture obviously, and it worked fine.

Perhaps a network problem?
 
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