使用VBA在电子邮件中插入文本正文和Excel表格正文

时间:2017-07-07 14:05:40

标签: excel vba excel-vba email outlook

The ideal format of the email I wish to automate. Certain parts blacked out for privacy.

我正在尝试创建可以使用VBA发送的商业用途的电子邮件模板,因为最终目标是用户只能通过userform填写空白,因此文本保持不变。我已经拥有了userform,并且已经编写了这样的方式,即在文本部分中填入了正确的空白并包含在电子邮件正文中,但是我还没有想出如何包含该表。

到目前为止,我尝试添加表格是

Sub SendEmail(what_address As String,subject_line As String,mail_body As String,claim_info As Range)

Dim olApp作为Outlook.Application 设置olApp = CreateObject(" Outlook.Application")

AndroidInjection.inject(this)

End Sub Sub SendClaimsEmail()

Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = what_address
olMail.Subject = subject_line
olMail.Body = mail_body
olMail.HTMLBody = RangeToHtml.claim_info
olMail.Send

MsgBox"完成!"

End Sub

1 个答案:

答案 0 :(得分:1)

我认为您的问题在于尝试在同一条消息中合并olMail.BodyolMail.HTMLBody。根据我的经验,您需要选择一个,表格不适用于olMail.Body

我建议将mail_body转换为有效的HTML并附加到olMail.HTMLBody

Sub SendEmail(what_address As String, subject_line As String, mail_body As String, claim_info As Range)

Dim olApp As Outlook.Application Set olApp = CreateObject("Outlook.Application")
Dim olApp As Outlook.Application Set olApp = 
CreateObject("Outlook.Application")

Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = what_address
olMail.Subject = subject_line
'assuming your mail_body uses vbLf for linefeeds you can just do this
mail_body = "<p>" & replace(mail_body,vblf,"</br>") & "</p>"
olMail.HTMLBody = mail_body & "</br>" & RangeToHtml.claim_info
olMail.Send
End Sub