Excel-VBA : Send Mail with Embedded Image in message body From MS Outlook using Excel.

In previous articles you have learned about Send a Simple Mail From MS Outlook Using Excel and how to send Attachment With the Mail MS Outlook Using Excel. ( I recommend you first read these articles to understand the basics if you are new to this)

In this tutorial you will learn how to add an image in the mail body or message and send it from Microsoft Outlook.

Ides is to attach the image in hidden manner and later add it to using image name in the HtmlBody.

See the Code.

Complete Code:

[sourcecode language=”VB”]
Sub sumit()
Dim mainWB As Workbook
Dim SendID
Dim Subject
Dim Body
Dim olMail As MailItem

Set otlApp = CreateObject("Outlook.Application")
Set olMail = otlApp.CreateItem(olMailItem)
Set Doc = olMail.GetInspector.WordEditor
‘Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment

Set mainWB = ActiveWorkbook

SendID = mainWB.Sheets("Mail").Range("B1").Value
CCID = mainWB.Sheets("Mail").Range("B2").Value
Subject = mainWB.Sheets("Mail").Range("B3").Value
Body = mainWB.Sheets("Mail").Range("B4").Value
With olMail
.To = SendID
If CCID <> "" Then
End If
.Subject = Subject
‘add the image in hidden manner, position at 0 will make it hidden
.Attachments.Add "C:\Users\Sumit Jain\Pictures\11\city.jpg", olByValue, 0

‘Now add it to the Html body using image name
‘change the src property to ‘cid:your image filename’
‘it will be changed to the correct cid when its sent.
.HTMLBody = .HTMLBody & "<br><B>Embedded Image:</B><br>" _
& "<img src=’cid:city.jpg’" & "width=’500′ height=’200’><br>" _
& "<br>Best Regards, <br>Sumit</font></span>"
End With

MsgBox ("you Mail has been sent to " & SendID)

End Sub


Send Mail with Embedded Image in message body-1

Send Mail with Embedded Image in message body-1

Send Mail with Embedded Image in message body-2

Send Mail with Embedded Image in message body-2

You may also like...

5 Responses

  1. John Gibbs says:

    Is there any way to align the image? I tried changing it to:

    But this just breaks it and I get nothing.

  2. Glenn says:

    Wow….love the way you coded this….simple and manageable. Thank you very much for sharing.

  3. Jaime says:

    This works really well if you’re using Outlook and I’ve tried this but when I send the message to anyone not using Outlook on a computer (mobile device, gmail, etc), they receive it as a blank image with the cid showing. Any idea on how to make this available throughout all e-mail services?

  4. Mateus says:

    That’s great! Many thanks!

Leave a Reply to Jaime Cancel reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: