温馨提示:本文翻译自stackoverflow.com,查看原文请点击:excel - How to add JPG file to oft template at specific location in on email body
excel outlook vba

excel - 如何在电子邮件正文中特定位置的oft模板中添加JPG文件

发布于 2020-04-09 10:07:02

我正在尝试通过使用Outlook oft模板分发电子邮件。在特定位置的模板上,我想附加从excel范围创建的jpg文件。

Dim OutApp As Object
Dim OutMail As Object
Dim str_jpeg_file as String
str_jpeg_file  = "B:\temp\test.jpg"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItemFromTemplate(ThisWorkbook.Path & "\Test.oft")
With OutMail
    .To = "test@abcd.com"
    .CC = ""
    .BCC = ""
    .Subject = "Test mail"
    .SentOnBehalfOfName = "zyz@abcd"
    .Attachments.Add str_jpeg_file, 1, 0
    .HTMLBody = Replace(.HTMLBody, "##IMAGE_PLACEHOLDER##", "<img src=""cid:test.jpg""height=520 width=750>")
    '.Send
    .display
End With

编辑:

jpg文件路径已更新,即 str_jpeg_file

查看更多

提问者
1S1a4m9
被浏览
43
Pᴇʜ 2020-01-31 21:57

给您一个完整的工作示例:

Option Explicit

Sub test()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim str_jpeg_file As String
    str_jpeg_file = "C:\Temp\test.png"
    Set OutApp = CreateObject("Outlook.Application")
    'Set OutMail = OutApp.CreateItemFromTemplate(ThisWorkbook.Path & "\Test.oft")
    'instead of a template I create a new mail
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = "test@abcd.com"
        .CC = ""
        .BCC = ""
        .Subject = "Test mail"
        .SentOnBehalfOfName = "zyz@abcd"
        .Attachments.Add str_jpeg_file, 1, 0
        'first we write some placeholder text so we can replace it
        .HTMLBody = "lalala ##IMAGE_PLACEHOLDER## lala"

        'replace
        .HTMLBody = Replace(.HTMLBody, "##IMAGE_PLACEHOLDER##", "<img src=""cid:test.png""height=256 width=256>")
        '.Send
        .display
    End With
End Sub

请注意,我使用了一封新电子邮件(无模板),因为它在此处更容易显示。

它完美地工作:

在此处输入图片说明

因此,如果它对您不起作用,则您的图像文件不是有效的图像,或者您做了其他错误的操作,例如错别字等,或者您的模板在某种程度上是问题所在。使用上面的代码再次检查。

最新帖子

最新文章