We help IT Professionals succeed at work.

Problem inserting HTML images into the body of a messge

Murray Brown
Murray Brown asked
Last Modified: 2012-05-10

I am using the code shown below to insert two images into the body of a message.
The iamges are coming up as blank squares as shown in the in the image I have attached.
What have I done wrong?
Thanks Image of email
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties
    Dim MYPIC_DIR As String: MYPIC_DIR = Environ("userprofile") & "\my documents"
    Dim olApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
    'Create e-mail item
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
       'Set body format to HTML
       .BodyFormat = olFormatHTML
       .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2>" _
                 & "<BODY>Please enter the message text here." _
                 & "<img src=""" & MYPIC_DIR & "\M1.jpg"" />" _
                  & "<img src=""" & MYPIC_DIR & "\M2.jpg"" />" _
                 & "</BODY></HTML>"

    End With

End Sub

Open in new window

Watch Question

Right click on the picture that you would like to insert the an Email, then click on properties.

In Adress: (URL) field, copy the full http link of the image for exampe,"http://xxx/xxxx/xxx.jpg"

In your email, click on insert picture, a windows pop up, in the file name field, paste the link; then click OK button.
Top Expert 2010

Hi, murbro.

I was concerned that wouldn't work.  When you embed an image in a message Outlook attaches the image as a hidden attachment and then creates a special link to it in the form of CID:xxxxx where xxxxx is the name of the hidden attachment.  If you're using Outlook 2003 or earlier, then the code on this page (http://www.outlookcode.com/d/code/htmlimg.htm) will embed an attachment.  If you're using 2007/2010, then it won't work because those versions of Outlook don't use CDO.  
Murray BrownASP.net/VBA/VSTO Developer


Hi. Thanks. I am using Outook 2010. What d you suggest I do. This is becoming a bit of a nightmare.
Top Expert 2010

I think I can modify the code to work with 2007.  I won't be able to get to that until tonight or perhaps tomorrow.
Murray BrownASP.net/VBA/VSTO Developer


Would be very grateful. Even if it takes a week. Thanks

In Office 2010, compose new Email, go to Insert tab; click on Picture icon.
Murray BrownASP.net/VBA/VSTO Developer


Thanks Iceradish, but I need this done programmatically ;-)
Top Expert 2010
Unlock this solution and get a sample of our free trial.
(No credit card required)
Murray BrownASP.net/VBA/VSTO Developer


Thanks very much. Really appreciate all the help and apologies for the late reply
Top Expert 2010

You're welcome!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.