vba vba电子邮件嵌入图像未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27544091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vba email embed image not showing
提问by John
I have an odd experience here. I have had loads of problems embedding a logo or image to emails using the src=cid... found out it wont show if not setting size for example.
我在这里有一个奇怪的经历。我在使用 src=cid 将徽标或图像嵌入电子邮件时遇到了很多问题...发现如果不设置大小,它就不会显示。
I have an Access application to send from, but have broken it down to the code below using Excel.
我有一个 Access 应用程序要发送,但已使用 Excel 将其分解为下面的代码。
BUT
但
It now works fine, if I display the email and then send it. Not doing anything else at all. Just display then send.
如果我显示电子邮件然后发送它,它现在可以正常工作。根本不做其他任何事情。只显示然后发送。
If I send directly from vba, the image will not display properly. The attachment symbol shows and Outlook itself will put the image inline, but say, gmail, wont. It's not gmails fault, as the attachmet symbol shows in Outlook. It doesn't if I display and then send.
如果我直接从 vba 发送,图像将无法正常显示。附件符号显示,Outlook 本身会将图像内联,但说,gmail,不会。这不是 gmail 的错,因为 Outlook 中显示了 attachmet 符号。如果我显示然后发送,则不会。
I suspect it's still something with sizing or placing. Without the width part, Outlook will still show the image at correct place, but still show as attachment. So when you display and press send then there must me another attribute set or something. I cannot find out what!
我怀疑它仍然与尺寸或放置有关。如果没有宽度部分,Outlook 仍会在正确的位置显示图像,但仍显示为附件。所以当你显示并按下发送时,我必须有另一个属性集或其他东西。我不知道什么!
Hope someone can help or has an idea! I'm not the strongest in HTLM, so it's probably something simple...
希望有人可以提供帮助或有想法!我不是 HTLM 中最强的,所以这可能很简单......
Thanks
谢谢
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 = "[email protected]"
oEmail.Subject = "test"
oEmail.Display
'oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
回答by John
Just to post the simple form of code that works. Big thanks to @Eugene Astafiev.
只是为了发布有效的简单代码形式。非常感谢@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 = "[email protected]"
oEmail.Subject = "test"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub
回答by Eugene Astafiev
You need to set PR_ATTACH_CONTENT_ID property (DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F) on the attachment using Attachment.PropertyAccessor. Be aware, the PropertyAccessor property of the Attachment class was added in Outlook 2007.
您需要使用 Attachment.PropertyAccessor 在附件上设置 PR_ATTACH_CONTENT_ID 属性(DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F)。请注意,附件类的 PropertyAccessor 属性是在 Outlook 2007 中添加的。
You may find the How do I embed image in Outlook Message in VBA?link helpful.
您可能会找到如何在 VBA 中的 Outlook 邮件中嵌入图像?链接有帮助。