在outlook邮件正文中嵌入图片excel vba

时间:2017-07-02 10:25:55

标签: excel vba excel-vba outlook

我正在尝试将工作表中的范围作为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

1 个答案:

答案 0 :(得分:6)

您需要添加图片并隐藏它。位置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

<强>截图

enter image description here