Solved

Problem inserting HTML images into the body of a messge

Posted on 2010-09-17
10
1,227 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Large Outlook files lead to various unwanted errors and corruption issues. Furthermore, large outlook files can also make Outlook take longer to start-up, search, navigate, and shut-down. So, In this article, i will discuss a method to make your Out…
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

756 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