如何使用VBA将图像嵌入到Outlook电子邮件中

时间:2019-10-30 01:09:31

标签: excel vba

Embed picture in outlook mail body excel vba密切相关

我正在尝试将图像嵌入到Outlook电子邮件中。

我正在使用以下代码段,其中一半是从上面的帖子中窃取的:

Sub PictureEmail()
    Dim outApp As New Outlook.Application
    Dim OutMail As Object
    Dim Attchmnt As String
    Dim Signature As String
    Dim WB As Workbook
    Set WB = ThisWorkbook
   Attchmnt = "C:\Users\Blah\Painted_Lady_Migration.jpg"


    Set OutMail = outApp.CreateItem(0)
    On Error Resume Next
    With OutMail
    .To = WB.Names("to").RefersToRange.Value2
    .CC = WB.Names("cc").RefersToRange.Value2
    .BCC = WB.Names("bcc").RefersToRange.Value2
    .Subject = WB.Names("Subject").RefersToRange.Value2
   .HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg""height=520 width=750>"
   .display
   End With

   If Attchmnt = "" Then
   Else
   OutMail.Attachments.Add Attchmnt
   End If

   On Error GoTo 0

End Sub

但是,当查看生成的电子邮件时,出现错误“无法显示链接的图像。该文件可能已被移动,重命名或删除”。

我尝试了几种附加文件的方法,包括:

.HTMLBody = "<img src=" & Chr(34) & "cid:Painted_Lady_Migration.jpg" & Chr(34) & "height=520 width=750>"

我只是无法使它工作> _ << / p>

我在某处看到名称/文件路径中的空格可以将其抛出,所以我用下划线替换了名称中的空格

我忘记/错过了什么愚蠢的事情?

1 个答案:

答案 0 :(得分:2)

cid是在您附加时创建的,因此需要在显示/发送它之前进行。

尝试一下

Set OutMail = outApp.CreateItem(0)
With OutMail
    .To = WB.Names("to").RefersToRange.Value2
    .CC = WB.Names("cc").RefersToRange.Value2
    .BCC = WB.Names("bcc").RefersToRange.Value2
    .Subject = WB.Names("Subject").RefersToRange.Value2
    If Attchmnt <> "" Then 
       .Attachments.Add Attchmnt ' (additional arguments are optional)
       .HTMLBody = "<img src=""cid:Painted_Lady_Migration.jpg"" height=520 width=750>"
    Else
       .HTMLBody = "[no attachment included]"
    End If
   .Display
End With