Why doesn't my image embed in an HTML email?

泄露秘密 提交于 2019-12-13 07:06:24

问题


I'm trying to run code in VBA that will send out HTML emails and am trying to embed an image within the email. I have the following code to do so:

Sub EmailImage()

Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment

Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")
oEmail.Close olSave
oEmail.To = "abc@abc123.com"
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:thumbs-up.jpg' align=baseline border=0>&nbsp;</BODY>"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing

End Sub

But what happens is that the image gets attached but there's a box with a red cross in it where the image should be, as is shown in the below image:

I have tried the following adapton to the code:

Sub EmailImage()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment

Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")

oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid")

oEmail.Close olSave
oEmail.To = "abc@abc123.com"
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:MyCid' align=baseline border=0>&nbsp;</BODY>"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub

But this gives me the error "Compile error: expected =" on the line oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid") so I tried to take out the , "MyCid") and changed the code in the email to just oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:thumbs-up.jpg' align=baseline border=0>&nbsp;</BODY>" again, so that it looks like this:

Sub EmailImage()

Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment

Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")
oAttach.PropertyAccessor.SetProperty ("http://schemas.microsoft.com/mapi/proptag/0x3712001F")

oEmail.Close olSave
oEmail.To = "abc@abc123.com"
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:thumbs-up.jpg' align=baseline border=0>&nbsp;</BODY>"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing

End Sub

But I now get the error Compile error: argument not optional with it pointing at the .SetProperty part of the line oAttach.PropertyAccessor.SetProperty ("http://schemas.microsoft.com/mapi/proptag/0x3712001F").

Please note that I am using Windows 7, Microsoft Excel 2010 and Microsoft Outlook 2010.


回答1:


Ok, after Googling the error message "Compile error: expected =", the changing the line:

oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid")

To:

Call oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid")

Worked.

I found this out from http://answers.microsoft.com/en-us/office/forum/office_2010-access/vba-compile-error-expect-in-sub-call/a47c752f-3034-41a6-a77c-b3ddaa34b24c?auth=1

Thank you for all your comments and answers.




回答2:


VBA does not like parenthesis when calling a sub - try to get rid of (): oAttach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid"



来源:https://stackoverflow.com/questions/33213518/why-doesnt-my-image-embed-in-an-html-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!