vba电子邮件嵌入图像未显示

时间:2014-12-18 10:12:44

标签: image vba email outlook embed

我在这里有一个奇怪的经历。我有很多问题使用src = cid将徽标或图像嵌入到电子邮件中...如果没有设置尺寸,就会发现它不会显示。

我有一个Access应用程序要发送,但是已经使用Excel将其分解为下面的代码。

BUT

现在它工作正常,如果我显示电子邮件然后发送它。根本没做任何其他事情。只需显示然后发送。

如果我直接从vba发送,图像将无法正常显示。附件符号显示和Outlook本身将图像内联,但说,gmail,不会。它不是电子邮件的错误,因为附件符号在Outlook中显示。如果我显示然后发送它就不会。

我怀疑它仍有尺寸或放置的东西。如果没有宽度部分,Outlook仍会在正确的位置显示图像,但仍显示为附件。因此,当您显示并按发送时,必须有我另一个属性集或其他东西。我找不到什么!

希望有人可以提供帮助或有想法!我不是HTLM中最强的,所以它可能很简单...

由于

约翰

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

2 个答案:

答案 0 :(得分:4)

您需要使用Attachment.PropertyAccessor在附件上设置PR_ATTACH_CONTENT_ID属性(DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F)。请注意,Attachment类的PropertyAccessor属性已添加到Outlook 2007中。

您可能会发现How do I embed image in Outlook Message in VBA?链接很有用。

答案 1 :(得分:4)

只是发布有效的简单代码形式。非常感谢@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