【问题标题】:Embed picture in outlook mail body excel vba在outlook邮件正文中嵌入图片excel vba
【发布时间】:2017-12-05 18:58:57
【问题描述】:

我正在尝试将工作表中的范围作为图像嵌入到 Outlook 邮件正文中。它正确保存了图片,但我只在 Outlook 邮件正文中看到空白图片。我在这里做错了什么?

Sub View_Email()

    tName = Trim(MAIN.Range("tEmail"))

    If Not tName Like "*@*.*" Then MsgBox "Invalid Email address": Exit Sub

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'File path/name of the gif file
    Fname = ThisWorkbook.Path & "\Claims.jpg"

    Set oCht = Charts.Add

    STAT.Range("A3:G26").CopyPicture xlScreen, xlBitmap
    With oCht
        .Paste
        .Export Filename:=Fname, Filtername:="JPG"
        '.Delete
    End With

    On Error Resume Next
    With OutMail
        .To = tName
        .CC = ""
        .BCC = ""
        .Subject = STAT.Range("C1").Value
        .HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
                    "<img src=" & Fname & "' height=520 width=750>"
        .display
        '.Send   'or use .Display
    End With
    On Error GoTo 0

    'Delete the gif file
    'Kill Fname

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

【问题讨论】:

  • 调试代码并检查Fname的值是多少,并手动检查文件是否存在。打开文件并检查图表是否导出
  • @SiddharthRout 是的,我确实检查了 fname 和图片,它们看起来都很好,但 Outlook 正文将图片显示为空白
  • 哦,我知道为什么...一会儿发布答案
  • 在看到损坏的图像/未在草稿电子邮件中显示后,我点击发送,图像在到达时正确显示。

标签: vba excel outlook


【解决方案1】:

您需要添加图像并将其隐藏。位置0 将添加和隐藏它。

.Attachments.Add Fname, 1, 0

1 是 Outlook 常量 olByValue

添加图片后,您必须使用"cid:FILENAME.jpg",如下所示。

试试这个

With OutMail
    .To = tName
    .CC = ""
    .BCC = ""
    .Subject = STAT.Range("C1").Value
    .Attachments.Add Fname, 1, 0
    .HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
                "<img src=""cid:Claims.jpg""height=520 width=750>"
    .Display
End With

截图

【讨论】:

  • 添加此内容以防任何人遇到与我相同的问题,请记住在 HTML 中,您的文件名中不能有空格 :) 直到我将“文件名”更改为“文件名”。
  • @hammythepig 实际上,你可以,但你只需要 encode 你的 URL 来处理文件名或路径中的空格。所以要使File Name有效,它应该被编码为File%20Name
  • 对于我的生活,我无法让它工作 - 中间的引号(“
  • @Selkie:你遇到了什么问题?
  • @SiddharthRout:我收到“无法显示图像,链接可能已损坏” - 我正在尝试动态引入文件名 (Temp_Picture_1.JPG),但我也尝试过只是字符串-我似乎无法找到所需的正确“”,并且复制粘贴示例导致编译器抱怨“-尝试了 chr(34) 替换,但仍然不行。我实际上是现在在家重新编写所有代码以发帖....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 2017-04-01
  • 2013-04-11
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
相关资源
最近更新 更多