vba email embed image not showing

前端 未结 2 866
死守一世寂寞
死守一世寂寞 2020-12-19 15:21

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.<

2条回答
  •  梦毁少年i
    2020-12-19 15:50

    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:\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 = " "
    
        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
    

提交回复
热议问题