VBA:如何在Outlook电子邮件中设置单行间距?
我正在从Excel中创建电子邮件。一旦电子邮件被创建,我需要在顶部添加一两条评论。我已经知道了如何设置字体样式,但是Outlook在回车时添加了一个双线空格,我真的不想这样。我怎样才能改变这个 代码如下:VBA:如何在Outlook电子邮件中设置单行间距?,vba,email,outlook,spacing,Vba,Email,Outlook,Spacing,我正在从Excel中创建电子邮件。一旦电子邮件被创建,我需要在顶部添加一两条评论。我已经知道了如何设置字体样式,但是Outlook在回车时添加了一个双线空格,我真的不想这样。我怎样才能改变这个 代码如下: Sub CreateDailyEmail() Dim oApp As Object Dim oMail As Object Set oApp = CreateObject("Outlook.Application") Set oMail = oApp.Cre
Sub CreateDailyEmail()
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = Range("EMAIL_TO")
.Cc = Range("EMAIL_CC")
.Subject = Range("EMAIL_SUBJECT")
.Attachments.Add (Range("PATH"))
.HTMLBody = "<p style=""font-family: Calibri; font-size: 14px; color: #00f; line-height: 1;""><br /></p>" & RangetoHTML(ActiveWorkbook.Worksheets("Daily").Range("B6:H65"))
.Display
End With
Set oMail = Nothing
Set oApp = Nothing
End Sub
Sub-CreateDailyEmail()
作为对象的Dim oApp
将oMail作为对象
设置oApp=CreateObject(“Outlook.Application”)
设置oMail=oApp.CreateItem(0)
与奥马尔
.To=范围(“电子邮件发送至”)
.Cc=范围(“电子邮件\u Cc”)
.Subject=范围(“电子邮件主题”)
.Attachments.Add(范围(“路径”))
.HTMLBody=“
“&RangetoHTML(ActiveWorkbook.Worksheets(“每日”).Range(“B6:H65”))
.展示
以
设置oMail=Nothing
设置oApp=Nothing
端接头
我不能完全确定我是否理解您的要求,因为我不确定回车是发生在excel工作表的单元格中,还是仅发生在outlook的html中,但我认为有两种方法:
可以替换单元格中的回车符和换行符(http://stackoverflow.com/questions/2321078/how-can-i-remove-blank-line-breaks-from-an-excel-cell-with-vb-or-a-formula
)处理间距问题,例如
Substitute(Substitute(A1, CHAR(10), ""), CHAR(13), "")
如果它发生在文档的html部分,则这是outlook特有的问题,因为使用您的设置的html示例效果很好:
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="A Jack Orange
Lantern Example" />
<style type="text/css">
p
{
font-family: "Calibri";
font-size: 20px;
color: #00f;
line-height: 1;
}
</style>
</head>
<title> What's Up? </title>
<body>
<p> This is totally a paragraph </p>
<p> this is totally a paragraph <br /> + a line
break </p>
<p> this is totally a paragraph with a line break
afterwards <br /> </p>
<p> Totally... yeah. <br /> </p>
</body>
</html>
或者,可能:
stringNewText = Replace(stringOldText, vbCr, <br>)
MSDN文档直接介绍了本Microsoft教程中有关使用项实体的替换功能:
vbaexpress上的教程似乎至少在外围解决了这个问题,并可能提供进一步的澄清:看起来您正在使用优秀的Ron De Bruin代码从Excel发送和发送电子邮件(因此使用RangetoHTML()公式) 我一直在使用这段代码,可以在 在.HTMLBody中使用body标记并将行高设置为1,而不是使用段落HTML标记。然后,当RangetoHTML返回您想要的范围时,它将只与您拥有的任何文本分隔一个空格! 这使得代码
Sub CreateDailyEmail()
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = Range("EMAIL_TO")
.Cc = Range("EMAIL_CC")
.Subject = Range("EMAIL_SUBJECT")
.Attachments.Add (Range("PATH"))
.HTMLBody = "<body style=""font-family: Calibri; font-size: 14px; color: #00f; line-height: 1;""><br />" & RangetoHTML(ActiveWorkbook.Worksheets("Daily").Range("B6:H65")) & "</body" & .HTMLBody
.Display
End With
Set oMail = Nothing
Set oApp = Nothing
End Sub
Sub-CreateDailyEmail()
作为对象的Dim oApp
将oMail作为对象
设置oApp=CreateObject(“Outlook.Application”)
设置oMail=oApp.CreateItem(0)
与奥马尔
.To=范围(“电子邮件发送至”)
.Cc=范围(“电子邮件\u Cc”)
.Subject=范围(“电子邮件主题”)
.Attachments.Add(范围(“路径”))
.HTMLBody=“
”和RangetoHTML(ActiveWorkbook.Worksheets(“每日”).Range(“B6:H65”)&“您能否分享一些输出HTML的示例(即HTMLBody
)作为参考?您有p
和br
标记,因此它会产生两个换行符-或者可能我不理解这个问题,因此+1例如HTML或问题的屏幕截图。
stringNewText = Replace(stringOldText, vbCr, vbCrLf)
Sub CreateDailyEmail()
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = Range("EMAIL_TO")
.Cc = Range("EMAIL_CC")
.Subject = Range("EMAIL_SUBJECT")
.Attachments.Add (Range("PATH"))
.HTMLBody = "<body style=""font-family: Calibri; font-size: 14px; color: #00f; line-height: 1;""><br />" & RangetoHTML(ActiveWorkbook.Worksheets("Daily").Range("B6:H65")) & "</body" & .HTMLBody
.Display
End With
Set oMail = Nothing
Set oApp = Nothing
End Sub