VBA Excel Outlook电子邮件正文格式 [英] VBA Excel Outlook Email Body Formatting
本文介绍了VBA Excel Outlook电子邮件正文格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个useform可以自动发送电子邮件。我想更改电子邮件的正文-其中一些将基于带有文本的单元格,因此它可以是动态的,而某些则将在代码中固定。目前-在运行时出现一个我需要的对象错误,我将非常感谢您的帮助。
我希望电子邮件正文中的每一行都分开。
I have useform that sends emails automatically. I want to change the body of the email - some of it will be based on a cell with text so it could be dynamic and some will be fixed in the code. for now - in runs me an error of object required, i'll be thankful for help. I want every line in the body of the email to be separate.
Sub sendMail(ByVal mail As String, name As String, Msht As Worksheet, CCmail As Integer, CCperson As String)
Dim applOL As Outlook.Application
Dim miOL As Outlook.MailItem
Dim recptOL As Outlook.Recipient
mailSub = Msht.Range("J2")
mailbody = Msht.Range("L2")
Set applOL = New Outlook.Application
Set miOL = applOL.CreateItem(olMailItem)
Set recptOL = miOL.Recipients.add(mail)
recptOL.Type = olTo
If CCmail = 1 Then
Set recptOL = miOL.Recipients.add(CCperson)
recptOL.Type = olCC
End If
tempPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.name
With miOL
.Subject = mailSub
.Body = "bla bla" & "bla bla bla" & mailbody.Font.Underline & Msht.Range("M2").Font.Bold & Body = Msht.Range("N2")
.Attachments.add (tempPath)
.send
End With
ActiveWorkbook.Close Savechanges:=True
Set applOL = Nothing
Set miOL = Nothing
Set recptOL = Nothing
End Sub
推荐答案
您需要将HTML格式应用于电子邮件正文:
You need to apply HTML format to the body of the email:
Dim body_ As String
body_= "<p> Hello </p>" & _
"<p> This is a line </p>" & _
"<p> This is another line </p>" & _
"<p> This is yet another line. </p>"
.BodyFormat = olFormatHTML
.HTMLBody = "<html><head></head><body>" & body_ & "</body></html>"
更新
Option Explicit
Public Sub sendMail(ByVal mail As String, name As String, Msht As Worksheet, CCmail As Integer, CCperson As String)
On Error GoTo ErrorTrap
Dim applOL As Outlook.Application
Set applOL = New Outlook.Application
Dim miOL As Outlook.MailItem
Set miOL = applOL.CreateItem(olMailItem)
Dim recptOL As Outlook.Recipient
Set recptOL = miOL.Recipients.Add(mail)
recptOL.Type = olTo
Dim mailSub As String
mailSub = Msht.Range("J2")
Dim mailbody As String
mailbody = "<p><u>" & Msht.Range("L2").Value & "</u></p>" & _
"<p><b>" & Msht.Range("M2").Value & "</b></p>" & _
"<p>" & Msht.Range("N2").Value & "</p>"
If CCmail = 1 Then
Set recptOL = miOL.Recipients.Add(CCperson)
recptOL.Type = olCC
End If
Dim tempPath As String
tempPath = ActiveWorkbook.Path & "\" & ActiveWorkbook.name
With miOL
.Subject = mailSub
.BodyFormat = olFormatHTML
.HTMLBody = "<html><head></head><body>" & mailbody & "</body></html>"
.Attachments.Add tempPath
.send
End With
ActiveWorkbook.Close Savechanges:=True
Leave:
On Error GoTo 0
Exit Sub
ErrorTrap:
MsgBox Err.Description, vbCritical
Resume Leave
End Sub
这篇关于VBA Excel Outlook电子邮件正文格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文