将图片内联附加在通过Excel通过VBA发送的电子邮件中

时间:2018-12-08 23:02:20

标签: excel vba outlook mime

我正在尝试使用以下VBA代码从excel发送多封电子邮件。这就是我被困住的地方。

由于某种原因,行Set attPr = attachment.PropertyAccessor-不断出现错误“对象不支持属性或方法”。

我还尝试将附件声明为显式附件对象(您可以看到该对象已被注释掉)-为此,我一直收到错误消息“未定义用户定义的类型”

正如我从构造的html正文(htmlTemp)中所看到的,我试图实现的目的只是将图像添加为电子邮件正文的一部分。

Sub Send_Email_Using_VBA_InlineBMPs()

Dim wb As Workbook
Dim sh As Worksheet

Set wb = ThisWorkbook

Dim Email_Subject, Email_Send_From, Email_Send_To, Email_Cc, Email_Body, Email_Attach As String
Dim htmlTemp, body1, body2 As String
Dim Mail_Object, Mail_Single, attachment, attPr As Variant
'Dim attachment As Outlook.Attachments
'Dim oPA As Outlook.PropertyAccessor

For Each sh In wb.Worksheets
    If sh.Name Like "Sheet 13*" Then
        Email_Subject = "HTML TEST - " & sh.Range("C129").Value
        Email_Send_From = "email@email.com"
        Email_Send_To = "email@email.com"
        Email_Cc = "email@email.com" '; 
        'Email_Body = sh.Range("C124").Value
        Email_Attach = ThisWorkbook.Path & "\" & sh.Range("R3").Value & ".pdf"
        Email_Picture = ThisWorkbook.Path & "\" & sh.Range("R3").Value & ".bmp"

        On Error GoTo debugs

        body1 = sh.Range("C124").Value
        body2 = Replace(body1, Chr(10), "<br>") 'to add newline
        Email_Body = body2

        'htmlTemp = "<!DOCTYPE html><html><body>"
        'htmlTemp = "<div id=email_body style='font-size: 12px; font-style: Arial'>"

        htmlTemp = "<div id=email_body>"
        htmlTemp = htmlTemp & Email_Body
        htmlTemp = htmlTemp & "<br><img src='cid:" & sh.Range("R3").Value & "' style='max-width: 100%; height: auto;'><br>"
        htmlTemp = htmlTemp & "</div>"

        'htmlTemp = htmlTemp & "</body></html>"

        Set Mail_Object = CreateObject("Outlook.Application")
        Set Mail_Single = Mail_Object.CreateItem(0)

        Set attachment = Mail_Single.Attachments '.Add(Email_Picture)
        attachment.Add Email_Picture
        Set attPr = attachment.PropertyAccessor
        'attachment.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", sh.Range("R3").Value

        With Mail_Single
            .Subject = Email_Subject
            .To = Email_Send_To
            .CC = Email_Cc
            '.body = htmlTemp
            .HTMLBody = htmlTemp
            '.Attachments.Add (Email_Attach)
            '.Attachments.Add Email_Picture, olByValue, 0
            .send
        End With

debugs:
        If Err.Description <> "" Then MsgBox Err.Description


    End If
Next


End Sub

注意:图片已经存在于文件夹中。

此处与解决方案的链接将我带到了这一点。 link 1 link 2
提前非常感谢

2 个答案:

答案 0 :(得分:0)

要将图像直接添加到HTML电子邮件正文,您必须使用以下HTML代码:  <img src='image name + extension'>

您必须执行以下操作:

  1. 将您的图片附加到电子邮件中
  2. 将此代码添加到您的HTMLbody:<img src='image name + extension'>
  3. 您可以添加一些参数,例如高度或宽度。
  4. 图片名称不能包含空格。

在下面您可以看到示例。

    Sub Sent_email_with_img()

    Dim my_email As MailItem

    Set out_look_app = CreateObject("Outlook.Application")

    Set my_email = out_look_app.CreateItem(my_item)

    With my_email
       .To = ""
       .CC = ""
       .Subject = ""

       ' here you have to attache img which you would see in email body
       ' attachment will be invisible
       .Attachments.Add Source:="PATH TO IMAGE\IMAGE_NAME.png"
       ' you can edit width and height of img in email body
       ' I changed width to 400 and height to 250
       .HTMLBody = "<p>This is my image</p>" _
                & "<img src='FILE_NAME.png'" _
                & "width=400 height=250>"
       ' display email
       .Display

      End With

    End Sub

答案 1 :(得分:-1)

根据我的研究,我发现了这篇文章Use VBA to Change the Name of Specific Attachment when the Send Button it Hit,并注意到了其中提到的内容:

如果使用Attachment.PropertyAccessor.SetProperty设置PR_ATTACH_LONG_FILENAME,Outlook将引发异常。您可以使用扩展MAPI来设置属性。