Insert Picture to HTML email

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Insert Picture to HTML email

Post by Abraxus »

I have a list of addresses I need to send e-mails to.

I also need to insert a picture into the e-mail (Insert, Picture).

I have code to do the e-mail creation and sending, but I can't figure out how to insert the picture...

Any suggestions?

Morgan
Morgan

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert Picture to HTML email

Post by HansV »

Do you use Automation to control Outlook from Access?
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Insert Picture to HTML email

Post by Abraxus »

Yes.

Here's my code:

Code: Select all

Sub SendAttachments()
    On Error Resume Next
    Dim oOutlookApp As Outlook.Application
    Dim oMailItem As Outlook.MailItem
    Dim strHTML As String
    strHTML = "Here are your attachments.<br><br>" & _
    "<b>Thank You!!<br>Your Friend<br><a href='http://www.cnn.com'>Visit CNN.com</a></b>"

    Set oOutlookApp = New Outlook.Application
    Set oMailItem = oOutlookApp.CreateItem(olMailItem)
    'Add E-mail addresses of Exception Handling Team Member
    Call oMailItem.Recipients.Add("you@youremail.com")
    Call oMailItem.Recipients.Add("anotheruser@email.com")
    
    oMailItem.Subject = "This is the title"
    oMailItem.HTMLBody = strHTML
    
 
    oMailItem.Attachments.Add "c:\path\to\the\file.txt"
    oMailItem.Send
End Sub
Morgan

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert Picture to HTML email

Post by HansV »

Adding a picture is VERY messy - see To add an embedded image to an HTML message in Microsoft Outlook using code. Problem is, it requires CDO, and this is no longer included with Outlook 2007 or later, so you can't count on it being available to everyone. Which version of Office are you using?
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Insert Picture to HTML email

Post by Abraxus »

2003
Morgan

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert Picture to HTML email

Post by HansV »

OK, it should work. Set a reference to the Microsoft CDO 1.21 Library (in Tools | References...).

Use the following version of the code. You must, of course, substitute the correct path of the picture to be inserted.

The code is adapted from the link I posted in my previous reply.

Code: Select all

Sub SendAttachments()
    On Error Resume Next
    Dim oOutlookApp As Outlook.Application
    Dim oMailItem As Outlook.MailItem
    Dim strHTML As String

    Dim oSession As MAPI.Session
    Dim oMsg As MAPI.Message
    Dim oAttachs As MAPI.Attachments
    Dim oAttach As MAPI.Attachment
    Dim colFields As MAPI.Fields
    Dim oField As MAPI.Field
    Dim strEntryID As String

    strHTML = "Here are your attachments.<br><br>" & _
    "<b>Thank You!!<br>Your Friend<br><a href='http://www.cnn.com'>Visit CNN.com</a></b>"

    Set oOutlookApp = New Outlook.Application
    Set oMailItem = oOutlookApp.CreateItem(olmailitem)
    ' Add E-mail addresses of Exception Handling Team Member
    Call oMailItem.Recipients.Add("you@youremail.com")
    Call oMailItem.Recipients.Add("anotheruser@email.com")

    oMailItem.Subject = "This is the title"
    oMailItem.HTMLBody = strHTML

    oMailItem.Attachments.Add "c:\path\to\the\file.txt"
    ' Substitute correct path
    oMailItem.Attachments.Add "c:\test\graphic.jpg"
    oMailItem.Close olSave
    strEntryID = oMailItem.EntryID
    Set oMailItem = Nothing
    
    ' Initialize CDO session
    On Error Resume Next
    Set oSession = CreateObject("MAPI.Session")
    oSession.Logon "", "", False, False
  
    ' Get the message created earlier
    Set oMsg = oSession.GetMessage(strEntryID)
    ' Set properties of the attached graphic that make
    ' it embedded and give it an ID for use in an <IMG> tag
    Set oAttachs = oMsg.Attachments
    Set oAttach = oAttachs.Item(2)
    Set colFields = oAttach.Fields
    Set oField = colFields.Add(CdoPR_ATTACH_MIME_TAG, "image/jpeg")
    Set oField = colFields.Add(&H3712001E, "myident")
    oMsg.Fields.Add "{0820060000000000C000000000000046}0x8514", 11, True
    oMsg.Update
  
    ' Get the Outlook MailItem again
    Set oMailItem = oOutlookApp.GetNamespace("MAPI").GetItemFromID(strEntryID)
    ' Add HTML content -- the <IMG> tag
    oMailItem.HTMLBody = oMailItem.HTMLBody & "<br><br><IMG align=baseline border=0 hspace=0 src=cid:myident>"
    oMailItem.Close olSave

    oMailItem.Send

    ' Clean up objects
    Set oField = Nothing
    Set colFields = Nothing
    Set oMsg = Nothing
    oSession.Logoff
    Set oSession = Nothing
End Sub
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Insert Picture to HTML email

Post by Abraxus »

Hmmm...only attached it rather than embedded it...
Morgan

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert Picture to HTML email

Post by HansV »

It worked for me...
x271.png
But the article I got the code from mentions that this sometimes happens - the author wasn't able to pinpoint the cause.

You might use

oMailItem.Display

instead of

oMailItem.Send

to inspect the result before it is sent.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert Picture to HTML email

Post by HansV »

Now that I look at it again - the code assumes that the picture is the second attachment. If your "real" code adds more regular attachments before adding the picture, the line

Set oAttach = oAttachs.Item(2)

should be modified accordingly.
Best wishes,
Hans

User avatar
Abraxus
3StarLounger
Posts: 254
Joined: 01 Mar 2010, 17:34
Location: Blue Springs, MO

Re: Insert Picture to HTML email

Post by Abraxus »

Changing
Set oAttach = oAttachs.Item(2)
to
Set oAttach = oAttachs.Item(1)
did the trick!

Thank you SO much!!
Morgan

JohnH
3StarLounger
Posts: 287
Joined: 09 Mar 2010, 23:16
Location: Canberra Australia

Re: Insert Picture to HTML email

Post by JohnH »

I was interested in the same question recently, so I tested (naively) what happens if I just used this code (with Access and Outlook 2007)

Code: Select all

    strLogo = "<p align='right'><img  src='" & strLogo & "' alt='logo'></p>"
Then I add this into the HTML string that becomes the message.
Even though strLogo initially contains the local path to the image, the image appears OK when I preview it, and appears when I send it someone else.
Outlooks seems to automatically convert it to this.

Code: Select all

    <p align=3Dright style=3D'text-align:right'><img width=3D36 height=3D26
id=3D"_x0000_i1025" src=3D"cid:image002.gif@01CB3548.CDBCEFE0" =
alt=3Dlogo><o:p></o:p></p>
Regards

John

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Insert Picture to HTML email

Post by HansV »

Interesting!
Best wishes,
Hans