在使用Excel VBA发送的电子邮件中内嵌图像

问题描述 投票:1回答:2

我正在尝试从Excel发送多封电子邮件。

[C0行出现错误

“对象不支持属性或方法”。

我尝试将附件声明为显式附件对象(您可以看到它已被注释掉)。我收到错误

“未定义用户定义的类型”

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

Set attPr = attachment.PropertyAccessor

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

这里与解决方案的链接将我带到了这一点。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 protected]" Email_Send_To = "[email protected]" Email_Cc = "[email protected]" '; '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

excel vba outlook mime
2个回答
0
投票

要直接将图像添加到HTML电子邮件正文,您必须使用以下HTML代码:link 2

您必须执行以下操作:

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

在下面您可以看到示例。

<img src='image name + extension'>

-1
投票

根据我的研究,我找到了这篇文章 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 ,并注意到了其中提到的内容:

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

© www.soinside.com 2019 - 2024. All rights reserved.