VBA:如何在 Outlook 电子邮件中设置单行间距?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11718343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA: How to set single line spacing in Outlook email?
提问by Richard H
I am creating an email from within Excel. Once the email's been created I need to add a comment or two at the top. I've figured out how to set the font style but Outlook is adding a double line space on Carriage Return which I really don't want. How can I change this?
我正在从 Excel 中创建电子邮件。创建电子邮件后,我需要在顶部添加一两条评论。我已经弄清楚如何设置字体样式,但 Outlook 正在回车上添加双行空间,这是我真正不想要的。我怎样才能改变这个?
Code below:
代码如下:
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
回答by Steve Irwin
It looks like you are using the excellent Ron De Bruin's code for sending and email from Excel (hence the RangetoHTML() formula).
看起来您正在使用出色的 Ron De Bruin 代码从 Excel 发送和发送电子邮件(因此使用 RangetoHTML() 公式)。
I have been using this same piece of code which can be found at http://www.rondebruin.nl/win/s1/outlook/mail.htm
我一直在使用同一段代码,可以在http://www.rondebruin.nl/win/s1/outlook/mail.htm找到
Instead of using paragraph HTML tags in the .HTMLBody use body tags and set the line height to 1. Then when the RangetoHTML returns the range you want it will only be seperated from any text you have by a single space! This makes the code....
而不是在 .HTMLBody 中使用段落 HTML 标签,而是使用正文标签并将行高设置为 1。然后当 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
回答by HymanOrangeLantern
I am not entirely sure I understand what you are asking, as I am unsure if the carriage returns are occurring in the cells from the excel sheet or only in the html in outlook, but there are two approaches in my mind:
我不完全确定我理解你在问什么,因为我不确定回车是发生在 Excel 工作表的单元格中还是只发生在 Outlook 的 html 中,但我认为有两种方法:
You can replace carriage returns and line breaks in a cell(http://stackoverflow.com/questions/2321078/how-can-i-remove-blank-line-breaks-from-an-excel-cell-with-vb-or-a-formula ) to deal with spacing issues, e.g.
您可以替换单元格中的回车和换行符(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), "")
If it is occuring in the html portion of your document, then it is an outlook-specific issue, as an html example using your settings works just fine:
如果它出现在文档的 html 部分,那么它是 Outlook 特定的问题,因为使用您的设置的 html 示例工作正常:
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="A Hyman 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>
For outlook, you should be able to replace carriage returns using the Replace function:
对于 Outlook,您应该能够使用 Replace 函数替换回车:
stringNewText = Replace(stringOldText, vbCr, "")
Or, possibly:
或者,可能:
stringNewText = Replace(stringOldText, vbCr, <br>)
Or, alternatively:
或者,或者:
stringNewText = Replace(stringOldText, vbCr, vbCrLf)
The MSDN documentation directly addresses the replacement function in this Microsoft tutorial on working with item bodies: http://msdn.microsoft.com/en-us/library/office/dd492012(v=office.12).aspx
MSDN 文档直接解决了本 Microsoft 教程中有关使用项目正文的替换功能:http: //msdn.microsoft.com/en-us/library/office/dd492012(v=office.12).aspx
A tutorial at vbaexpress appears to at least peripherally address this issue, and may provide further clarification: http://www.vbaexpress.com/forum/showthread.php?t=39348
vbaexpress 的教程似乎至少在外围解决了这个问题,并且可能会提供进一步的说明:http://www.vbaexpress.com/forum/showthread.php?t=39348