首页 文章

使用vba从excel发送的电子邮件中内嵌图像

提问于
浏览
0

我坚持住了 .

出于某种原因,该行 - Set attPr = attachment.PropertyAccessor - 一直给我错误"Object does not support property or method" .

我还尝试将附件声明为一个显式的附件对象(你可以看到它被注释掉了) - 为此,我不断收到错误“用户定义的类型未定义”

我正在尝试实现的,正如您从我构建的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

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

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

  • -1

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

    你必须做以下事情:

    • 将您的图片附加到电子邮件中

    • 将此代码添加到HTMLbody: <img src='image name + extension'>

    • 您可以添加一些参数,如高度或宽度 .

    • 图像名称不能包含空格 .

    下面你可以看到例子 .

    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
    

相关问题