Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem inserting HTML images into the body of a messge

Posted on 2010-09-17
10
Medium Priority
?
1,263 Views
Last Modified: 2012-05-10
Hi

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>"
      

       .Display
    End With

End Sub

Open in new window

0
Comment
Question by:Murray Brown
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Expert Comment

by:iceradish
ID: 33703666
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.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33703678
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.  
0
 

Author Comment

by:Murray Brown
ID: 33703699
Hi. Thanks. I am using Outook 2010. What d you suggest I do. This is becoming a bit of a nightmare.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

by:David Lee
ID: 33703744
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.
0
 

Author Comment

by:Murray Brown
ID: 33703861
Would be very grateful. Even if it takes a week. Thanks
0
 
LVL 2

Expert Comment

by:iceradish
ID: 33703882
In Office 2010, compose new Email, go to Insert tab; click on Picture icon.
0
 

Author Comment

by:Murray Brown
ID: 33704314
Thanks Iceradish, but I need this done programmatically ;-)
0
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 33827006
murbro,

Sorry to have taken so long.  Clearly it was more than a day or even a week.  

This code is the 2007 version of that in the article I linked to.  It appears to work properly.  
Sub EmbeddedHTMLGraphic2007()
    Const PR_ATTACH_MIME_TAG = "http://schemas.microsoft.com/mapi/proptag/0x370E001E"
    Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
    Dim olkMsg As Outlook.MailItem, _
        olkAtt As Outlook.Attachment, _
        olkPA As Outlook.PropertyAccessor, _
        strPicDir As String
    strPicDir = Environ("userprofile") & "\my documents"
    Set olkMsg = Application.CreateItem(olMailItem)

    'Attach the first picture'
    Set olkAtt = olkMsg.Attachments.Add(strPicDir & "\M1.jpg")
    Set olkPA = olkAtt.PropertyAccessor
    olkPA.SetProperty PR_ATTACH_MIME_TAG, "image/jpeg"
    olkPA.SetProperty PR_ATTACH_CONTENT_ID, "myident1"

    'Attach the second picture'
    Set olkAtt = olkMsg.Attachments.Add(strPicDir & "\M2.jpg")
    Set olkPA = olkAtt.PropertyAccessor
    olkPA.SetProperty PR_ATTACH_MIME_TAG, "image/jpeg"
    olkPA.SetProperty PR_ATTACH_CONTENT_ID, "myident2"

    With olkMsg
       .BodyFormat = olFormatHTML
       .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2>" _
                 & "<BODY>Please enter the message text here." _
                 & "<IMG src=cid:myident1 />" _
                 & "<IMG src=cid:myident2 />" _
                 & "</BODY></HTML>"
       .Display
    End With

    olkMsg.Display
    Set olkMsg = Nothing
    Set olkAtt = Nothing
    Set olkPA = Nothing
End Sub

Open in new window

0
 

Author Closing Comment

by:Murray Brown
ID: 33862280
Thanks very much. Really appreciate all the help and apologies for the late reply
0
 
LVL 76

Expert Comment

by:David Lee
ID: 33862630
You're welcome!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question