Vba email embed image not showing
I have a strange experience here. I had a lot of problems with attaching a logo or image to emails using src = cid ... it turned out that it wouldn't display unless the size was set, for example.
I have an Access app to submit, but it was broken down into the code below using Excel.
BUT
Now it works fine if I show the email and send it. Do nothing at all. Just show, then send.
If I post directly from vba the image is not displayed correctly. The anchor symbol is displayed and Outlook itself will put the image in the string, but say gmail won't. This does not cause an error as the attachment symbol is displayed in Outlook. This does not mean that I will display and submit.
I suspect this is something else with sizing or placement. Without part of the width, Outlook will still show the image in the correct place, but it will still show as an attachment. So when you show and hit send, then I need to set another attribute or whatever. I can't figure out what!
Hope someone can help or have an idea! I'm not the strongest at HTLM, so maybe something simple ...
thank
John
Sub test()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
'create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
'add graphic as attachment to Outlook message
'change path to graphic as needed
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\temp\logo.jpg")
oEmail.Close olSave
'change the src property to 'cid:your picture filename'
'it will be changed to the correct cid when its sent.
oEmail.HTMLBody = "<BODY><IMG src=""cid:logo.jpg"" width=200> </BODY>"
oEmail.Save
oEmail.To = "someemailtogoinhere@gmail.com"
oEmail.Subject = "test"
oEmail.Display
'oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
source to share
You need to set the PR_ATTACH_CONTENT_ID (DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F ) property in your application using Attachment.PropertyAccessor. Remember that the PropertyAccessor property of the Attachment class was added in Outlook 2007.
You can find How do I add an image to an Outlook message in VBA? ...
source to share
Just post a simple form of code that works. Many thanks to @Eugene Astafiev.
Sub test()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment
Dim olkPA As Outlook.PropertyAccessor
Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
'create new Outlook MailItem
Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
'add graphic as attachment to Outlook message
'change path to graphic as needed
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\temp\logo.jpg")
Set olkPA = oAttach.PropertyAccessor
olkPA.SetProperty PR_ATTACH_CONTENT_ID, "logo.jpg"
oEmail.Close olSave
'change the src property to 'cid:your picture filename'
'it will be changed to the correct cid when its sent.
oEmail.HTMLBody = "<BODY><IMG src=""cid:logo.jpg""> </BODY>"
oEmail.Save
oEmail.To = "someemail@gmail.com"
oEmail.Subject = "test"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
source to share