【问题标题】:Pictures pasted on the body of outlook email are not displayed粘贴在outlook邮件正文上的图片不显示
【发布时间】:2020-05-01 14:53:43
【问题描述】:

我使用下面的代码从文件中复制一个范围并将其作为图片粘贴到电子邮件中,但有一个问题:如果您在 .send 之前不使用 .display,图片将不会显示给接收者. 有谁知道解决这个问题的方法?只是为了避免 Outlook 窗口在屏幕上闪烁。

Sub sendMail()

    Dim olApp As Object
    Dim NewMail As Object
    Dim ChartName As String
    Dim imgPath As String

    Set olApp = CreateObject("Outlook.Application")
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    tmpImageName = VBA.Environ$("temp") & "\tempo.jpg"

    Workbooks.Open "C:\FilePath\File.xlsm"
    Set RangeToSend = Workbooks("File.xlsm").Sheets(Name).Range(" ")

    RangeToSend.CopyPicture Appearance:=xlScreen, Format:=xlPicture

    Set sht = Sheets.Add
    sht.Shapes.AddChart
    sht.Shapes.Item(1).Select
    Set objChart = ActiveChart

    With objChart
        .ChartArea.Height = RangeToSend.Height
        .ChartArea.Width = RangeToSend.Width
        .ChartArea.Fill.Visible = msoFalse
        .ChartArea.Border.LineStyle = xlLineStyleNone
        .Paste
        .Export Filename:=tmpImageName, FilterName:="JPG"
    End With

    sht.Delete

    Workbooks("File.xlsm").Close

    Set NewMail = olApp.CreateItem(0)

    With NewMail
        .Subject = "Latest performance report" ' Replace this with your Subject
        .To = "email@email.com" ' Replace it with your actual email
        .HTMLBody = "<span LANG=EN>" _
            & "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
            & "Hello, this is the data range that you want:<br> " _
            & "<br>" _
            & "<img src=" & "'" & tmpImageName & "'/>" _
            & "<br>" _
            & "<img src=" & "'" & tmpImageName2 & "'/>" _
            & "<br>" _
            & "<br>Best Regards!</font></span>"
        .Display
        .Send

        Set olApp = Nothing
        Set NewMail = Nothing
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True

    End With


End Sub

【问题讨论】:

  • 你试过设置正文格式.BodyFormat = olFormatHTML吗?
  • 你见过THIS

标签: excel vba outlook


【解决方案1】:

看起来您正在将在 Excel 中捕获的图片保存到磁盘。然后您指的是新创建的项目正文中的图像。但是图像源仍然指向您磁盘上的文件。因此,收件人永远不会正确显示它。

相反,您需要附加一个文件,然后在邮件正文中添加一个引用。

Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E"        
Const PR_ATTACHMENT_HIDDEN = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B" 

...

Set colAttach = mail.Attachments        
Set l_Attach = colAttach.Add(path_to_the_file)            
Set oPA = l_Attach.PropertyAccessor            

oPA.SetProperty PR_ATTACH_CONTENT_ID, "itemID"            
oPA.SetProperty PR_ATTACHMENT_HIDDEN, True        

那么你可以通过以下方式修改消息体:

.HTMLBody = "<span LANG=EN>" _
        & "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
        & "Hello, this is the data range that you want:<br> " _
        & "<br>" _
        & "<img src='cid:itemID'/>" _
        & "<br>" _
        & "<br>Best Regards!</font></span>"

    .Send

【讨论】:

  • 感谢您的回复。这是一个不错的选择,虽然我仍然不明白为什么我可以在发送前显示 Outlook 窗口(使用 .display 命令)来发送电子邮件正文中的图片(不附加文件)。
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 2015-06-04
  • 2020-04-05
  • 2021-01-22
  • 1970-01-01
  • 2010-09-13
  • 2019-08-02
相关资源
最近更新 更多