Solved

Problem inserting HTML images into the body of a messge

Posted on 2010-09-17
10
1,231 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:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:murbro
ID: 33703699
Hi. Thanks. I am using Outook 2010. What d you suggest I do. This is becoming a bit of a nightmare.
0
To Patch or not to Patch? That is the question!

Don't get caught out like thousands of others around the world in the recent Ransomware Fiasco!
Discuss..
- Why it's not a good idea to wait before Patching
- Sensible approaches to Patching discussed
- Add your feedback, comments and suggestions

 
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:murbro
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:murbro
ID: 33704314
Thanks Iceradish, but I need this done programmatically ;-)
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 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:murbro
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

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!

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Read this checklist to learn more about the 15 things you should never include in an email signature.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
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…

751 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