Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

vba email embed image not showing

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.

I have an Access application to send from, but have broken it down to the code below using 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.

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!

Hope someone can help or has an idea! I'm not the strongest in HTLM, so it's probably something simple...

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:emplogo.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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just to post the simple form of code that works. Big 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:emplogo.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...