Solved

Problem inserting HTML images into the body of a messge

Posted on 2010-09-17
10
1,211 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Use email signature images to promote corporate certifications and industry awards.
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now