VBA – Insert Inline Image – Into Email Bo0dy
Wondering how to attach inline images in Outlook email?
It can be done by converting the default text or rich text email to a HTML email format.
After that, with the help of some HTML code in Email.HTMLBody we can create beautiful emails with some appealing media.
Outlook VBA Code – Embed Image – in Email Body
We have the HTML code, but the image file is in the computer folder. How to we attach it in the email?
For that, we use Attachment.add method with few additional parameters. Then we refer the image files name in <img cid:> tag within HTML code.
Outlook will automatically convert the image filename into CID (Content Identifier) code.
Note: Do not forget to add reference to Microsoft Outlook Object Library
Lets see how it is done using the Outlook or Excel VBA code.
Sub Outlook_Email_With_Inline_Image() 'Add reference to Microsoft Outlook Object Library Dim OutApp As Outlook.Application Dim oOutlookEmail As Outlook.MailItem 'Create New Outlook Email Item to Attach Image(s) Set OutApp = CreateObject("Outlook.Application") Set oOutlookEmail = OutApp.CreateItem(0) 'Actual Excel VBA to send email with Embedded images With oOutlookEmail .To = "email@example.com" .CC = "" .BCC = "" .Subject = "Happy New Year" .BodyFormat = olFormatHTML .Attachments.Add "D:\ImageFile.img", olByValue, 0 sImgName = "ImageFile.img" .HTMLBody = "<img src='cid:" & sImgName & "'" & " ><br>" 'Mention only the image file name not its path 'Or Use this below line. '.HTMLBody = "<img src='" & sImgName & "'" & " ><br>" .Display .Send 'or just put .Display to check End With Set OutlookMail = Nothing Set OutApp = Nothing End Sub
In the attachment.add we add additional parameters to hide the image file from appearing as an attachment.
Inline Images in Email – Additional Details
There is lot of discussion on whether using the CID would be good enough. Many suggest that this method is not consistent with all type of email clients. But there is not much of option here.
Using this technique we can add Inline images, background images, smileys, GIF images etc.,
In case if you are new to sending emails using VBA code, then to know bit of basic email automation using Outlook refer this article that has simple vba code.
Additional Reference: Read this article to know about alternate methods available.