将图像(图表)添加到 HTMLbody

Add image (chart) to HTMLbody

我在将图像(图表)插入 HTMLbody 时遇到问题。我把图表导出到另一个文件夹,然后调用图片路径。

msg = "<html>123,<br/> 123 <b>" & countries & ":</b><br/>" & RangetoHTML(tablex) & s & "<img src=""cid:" & fileName & "><html\>"

然而,当我插入带有上述消息正文的图像后,它显示:

在通过正确指定图像进行调整后,我得到:

要指定我使用的确切图像:

Set myChart = wbe.Sheets("Sheet1").ChartObjects("Chart 11").Chart

Dim myPicture As String
Dim fileName As String
Dim myPath As String

myPicture = "Chart1.jpg"
myPath = "C:\qwe\"

fileName = myPath & myPicture
myChart.Export fileName

完整代码:

Sub transactional_emails()

'Create email and save it as draft
    Dim olApp As Object
    Dim olMailItm As Object
    Dim iCounter As Integer
    Dim Dest As Variant
    Dim SDest As String
    Dim source As String
    Dim oAccount As String
    Dim msg As String
    Dim tablex As Range
    Dim wbe As Workbook
    Dim las As Long
    Dim countries As String
    Dim myChart As Chart
    
    countries = "LOL"
    
    Set wbe = Workbooks(ThisWorkbook.Name)
    
    las = wbe.Sheets("Sheet1").Cells(wbe.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Row
    
    Set tablex = wbe.Sheets("Sheet1").Range("A1:G" & las)
    
    With tablex.Borders
        .LineStyle = xlContinuous
        .Color = vbBlack
        .Weight = xlThin
    End With
    
    
    Set myChart = wbe.Sheets("Sheet1").ChartObjects("Chart 11").Chart
    
    Dim myPicture As String
    Dim fileName As String
    Dim myPath As String

    myPicture = "Chart1.jpg"
    myPath = "C:\qwe\"

    fileName = myPath & myPicture
    myChart.Export fileName
    

    'Create the Outlook application and the empty email.
    Set olApp = CreateObject("Outlook.Application")
    Set olMailItm = olApp.CreateItem(0)
    
    Dim s As String
    s = Environ("appdata") & "\Microsoft\Signatures\"
    If Dir(s, vbDirectory) <> vbNullString Then s = s & Dir$(s & "*.htm") Else s = ""
    s = CreateObject("Scripting.FileSystemObject").GetFile(s).OpenAsTextStream(1, -2).ReadAll
    
    msg = "<html>123,<br/> 123 <b>" & countries & ":</b><br/>" & RangetoHTML(tablex) & s & "<img src=""cid:" & myPicture & """></html>"
    
    With olMailItm
        SDest = "gal.bordelius_ext@novartis.com"
        'oAccount = "customer.service_GOC@novartis.com"
    
        .To = SDest
        .CC = "gal.bordelius_ext@novartis.com"
        .Subject = countries & " 123 " & Date
        .Attachments.Add fileName, 1, 0
        .htmlbody = msg
        .Save
    End With
    
    'Clean up the Outlook application.
    Set olMailItm = Nothing
    Set olApp = Nothing

End sub

源代码有效:

  1. 您将图像附加到 Outlook 中的邮件项目:
.Attachments.Add fileName, 1, 0
  1. 那么您可以通过以下方式从邮件正文中引用附件图片:
"<img src=""cid:" & myPicture & """>
  1. 有时您还需要使用 Attachment.PropertyAccessor.
  2. 在附件上设置 PR_ATTACH_CONTENT_ID 属性 (DASL - http://schemas.microsoft.com/mapi/proptag/0x3712001F)

您也可以考虑添加一些参数,例如高度或宽度。注意,图片名称不能包含空格。

当您在 Microsoft Office Outlook 中打开包含图像的电子邮件时,图像区域可能会被阻止。在 Pictures cannot be displayed and are shown as red X in Outlook 文章中阅读更多相关信息。