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 = "officetricks123@gmail.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.