Re: How do I embed image in Outlook Message in VBA?

  • Thread starter Ken Slovak - [MVP - Outlook]
  • Start date
L

Lu

Hi, that's the page where I got the sample code for embedding images and that
didn't work because I can't get the right ocx/dll libraries they are using.

Do you have any links/code sample for Outlook 2007 that I can look at? thanks.
 
K

Ken Slovak - [MVP - Outlook]

CDO 1.21 for Outlook 2007 is a separate download from MS. If you have
downloaded that and set a VBA project reference to Microsoft CDO 1.21
Library then you should have no trouble at all with the code at that link.

I'd never bothered translating that CDO code into Outlook 2007 code using
Attachment.PropertyAccessor, but on the fly it would look something like
this for Outlook VBA:

Sub EmbeddedHTMLGraphicDemo()
' Outlook objects
Dim oApp as Outlook.Application
Dim oMsg As Outlook.MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
Dim oPA As Outlook.PropertyAccessor

' not an URL, a DASL property tag string
Const PR_ATTACH_MIME_TAG =
"http://schemas.microsoft.com/mapi/proptag/0x370E001E"

Const PR_ATTACH_CONTENT_ID = "urn:schemas:mailheader:content-id"

' not an URL, a DASL property tag string
Const PR_HIDE_ATTACH = _
"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B"

On Error Resume Next

' create new Outlook MailItem
' Use CreateObject for Outlook.Application if not in OL VBA
Set oApp = Application
Set oMsg = oApp.CreateItem(olMailItem)
' add graphic as attachment to Outlook message
' change path to graphic as needed
Set colAttach = oMsg.Attachments
Set oAttach = colAttach.Add("c:\test\graphic.jpg")
oMsg.Save

' *** POSITION CRITICAL *** you must dereference the
' attachment objects before changing their properties
Set colAttach = Nothing
Set oAttach = Nothing

' set properties of the attached graphic that make
' it embedded and give it an ID for use in an <IMG> tag
Set colAttach = oMsg.Attachments
Set oAttach = colAttach.Item(1)
Set oPA = oAttach.PropertyAccessor
oPA.SetProperty PR_ATTACH_MIME_TAG, "image/jpeg"
oPA.SetProperty PR_ATTACH_CONTENT_ID, "myident"
Set oPA = Nothing

Set oPA = oMsg.PropertyAccessor
oPA.SetProperty PR_HIDE_ATTACH, True
oMsg.Save

oMsg.HTMLBody = "<IMG align=baseline border=0 hspace=0 src=cid:myident>"
oMsg.Close olSave
oMsg.Display

' clean up objects
Set colAttach = Nothing
Set oAttach = Nothing
Set oPA = Nothing
Set oMsg = Nothing
Set oApp = Nothing
End Sub
 
L

Lu

Thank you! I will try it out.

Ken Slovak - said:
CDO 1.21 for Outlook 2007 is a separate download from MS. If you have
downloaded that and set a VBA project reference to Microsoft CDO 1.21
Library then you should have no trouble at all with the code at that link.

I'd never bothered translating that CDO code into Outlook 2007 code using
Attachment.PropertyAccessor, but on the fly it would look something like
this for Outlook VBA:

Sub EmbeddedHTMLGraphicDemo()
' Outlook objects
Dim oApp as Outlook.Application
Dim oMsg As Outlook.MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
Dim oPA As Outlook.PropertyAccessor

' not an URL, a DASL property tag string
Const PR_ATTACH_MIME_TAG =
"http://schemas.microsoft.com/mapi/proptag/0x370E001E"

Const PR_ATTACH_CONTENT_ID = "urn:schemas:mailheader:content-id"

' not an URL, a DASL property tag string
Const PR_HIDE_ATTACH = _
"http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B"

On Error Resume Next

' create new Outlook MailItem
' Use CreateObject for Outlook.Application if not in OL VBA
Set oApp = Application
Set oMsg = oApp.CreateItem(olMailItem)
' add graphic as attachment to Outlook message
' change path to graphic as needed
Set colAttach = oMsg.Attachments
Set oAttach = colAttach.Add("c:\test\graphic.jpg")
oMsg.Save

' *** POSITION CRITICAL *** you must dereference the
' attachment objects before changing their properties
Set colAttach = Nothing
Set oAttach = Nothing

' set properties of the attached graphic that make
' it embedded and give it an ID for use in an <IMG> tag
Set colAttach = oMsg.Attachments
Set oAttach = colAttach.Item(1)
Set oPA = oAttach.PropertyAccessor
oPA.SetProperty PR_ATTACH_MIME_TAG, "image/jpeg"
oPA.SetProperty PR_ATTACH_CONTENT_ID, "myident"
Set oPA = Nothing

Set oPA = oMsg.PropertyAccessor
oPA.SetProperty PR_HIDE_ATTACH, True
oMsg.Save

oMsg.HTMLBody = "<IMG align=baseline border=0 hspace=0 src=cid:myident>"
oMsg.Close olSave
oMsg.Display

' clean up objects
Set colAttach = Nothing
Set oAttach = Nothing
Set oPA = Nothing
Set oMsg = Nothing
Set oApp = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top