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

标签 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>"

我就是无法让它工作>_<

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

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

最佳答案

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

关于excel - 如何使用 VBA 将图像嵌入到 Outlook 电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58617458/

相关文章:

vba - 在 Word VBA 中使用 .body 而不是 .htmlbody 将我的电子邮件正文的一部分设为粗体

excel - 根据单元格的值删除一行

vba - 将 *.xlsm 文件另存为 *.xlsx 并抑制弹出窗口

Excel VBA 将项目添加到组合框而不重复项目

vba - 引用表的 .DataBodyRange 的特定范围

VBA:范围方法失败

excel - 用基于另一个单元格值的非空白单元格填充空白单元格值

excel - Playframework Excel 文件生成

excel - 通过更改类名进行抓取

vba - 在mail.body中查找 "carriage return"