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

2019-09-10 10:51发布

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:

enter image description here

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.

2条回答
地球回转人心会变
2楼-- · 2019-09-10 10:53

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"

查看更多
来,给爷笑一个
3楼-- · 2019-09-10 11:01

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.

查看更多
登录 后发表回答