excel 使用VBA在电子邮件中插入图像

wko9yo5t  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(278)

我在Excel中运行了以下VBA代码片段,它会向我们的客户发送电子邮件。我正尝试在电子邮件正文中插入一张图片。下面的代码在发送到我的内部Outlook电子邮件时工作正常,但在发送到我的个人Gmail帐户时不工作,这意味着图像不会显示。可能是因为信头文件路径在我们的网络中?请提供帮助,不胜感激!

Dim MItem As Outlook.MailItem
Dim LetterHead As String
LetterHead = "file path to .png file located in company network drive"

For x = 1 To xlList.DataBodyRange.Rows.Count
    Set MItem = OutlookApp.CreateItem(olMailItem)
    PolNum = xlList.DataBodyRange.Cells(x, 7).Value
    PolSubj = Subj & PolNum
    
    With MItem
        .To = EmailAddr
        .Subject = PolSubj
        .HTMLBody = "<center><img src=" & Chr(34) & LetterHead & Chr(34) & "></center>" & StrBody & .HTMLBody
        .Send
    End With
mec1mxoz

mec1mxoz1#

尝试

Option Explicit

Sub InsertImageInEmailBody()

    Dim ImageFilePath As String, xMailBody As String
    
    ' Set the image file path
    ImageFilePath = "C:\path\to\file\picture.png"
    
    xMailBody = "<BODY>Hi,<br> <br>This is the pic<br><br>" & _
        "<img src='" & ImageFilePath & "'><br><br>" & _
        "Thank you.</BODY>"
    
With CreateObject("outlook.application").CreateItem(0)
        .To = "someone@somewhere.com"
        .Subject = "Here it is"
        .HTMLBody = xMailBody & .HTMLBody
        '.Attachments.Add
        .Display
        '.send
End With
    
    
End Sub
svdrlsy4

svdrlsy42#

也许是因为信头文件路径在我们的网络内?
是的,您走对了路。您需要将图像上传到任何网络服务器,以便其他人使用。请注意,电子邮件客户端应用程序可能会自动阻止互联网图像。
如果您需要确保向用户显示图像,则需要将图像附加到邮件项目(请参见Attachments.Add),然后使用PropertyAccessor.SetProperty设置PR_ATTACH_CONTENT_ID MAPI属性(DASL名称为“http://schemas.microsoft.com/mapi/proptag/0x3712001F“),并使用与附件对象上设置的PR_ATTACH_CONTENT_ID属性值相匹配的src属性引用附加的文件。例如:

attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "YourPictureId")
MailItem.HTMLBody = "<html><body>This is an embedded image <img src=""cid:YourPictureId""></body></html>"

PR_ATTACH_CONTENT_ID属性对应于发送邮件时的Content-ID MIME标头。

相关问题