Solved

Problem inserting HTML images into the body of a messge

Posted on 2010-09-17
10
1,217 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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