vba . 无论使用什么换行命令,创建 Outlook 电子邮件时的正文都是双倍行距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17348970/
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
.Body text when creating Outlook email is double-spacing no matter the new line command used
提问by Tim.McNamara
I'm creating an Outlook e-mail through VBA that runs off an Access form button click. For the .Body, I'm setting my 'strBody' string object using the following concatenated string:
我正在通过 VBA 创建 Outlook 电子邮件,该电子邮件通过 Access 表单按钮单击运行。对于 .Body,我正在使用以下连接字符串设置我的 'strBody' 字符串对象:
strBody = "First Line of text here." & vbNewLine & _
vbNewLine & _
"Second Line of text here." & vbNewLine & _
Chr(187) & "Third Line of text here." & vbNewLine & vbTab & _
Chr(187) & "Fourth Line of text here." & vbNewLine & vbTab & _
Chr(187) & "Fifth Line of text here." & vbNewLine & vbTab & vbTab & _
Chr(187) & "Sixth Line of text here." & vbNewLine & vbTab & _
Chr(187) & "Seventh Line of text here." & vbNewLine & _
"Eighth Line of text here"
The resulting text in the e-mail body should be:
电子邮件正文中的结果文本应为:
First Line of text here
第一行文字在这里
Second Line of text here.
Third Line of text here.
Fourth Line of text here.
Fifth Line of text here
Sixth Line of text here.
Seventh Line of text here.
Eighth Line of text here.
第二行文字在这里。
第三行文字在这里。
第四行文字在这里。
此处为第五行文字 此处为
第六行文字。
第七行文字在这里。
第八行文字在这里。
And when debug.print(ing), it looks perfect in the immediate window. However, when it is created in the Outlook window, it double-spaces every line so you get this:
当 debug.print(ing) 时,它在直接窗口中看起来很完美。但是,当它在 Outlook 窗口中创建时,它会将每一行双倍空格,以便您得到:
First Line of text here
第一行文字在这里
Second Line of text here.
第二行文字在这里。
Third Line of text here.
第三行文字在这里。
Fourth Line of text here.
第四行文字在这里。
Fifth Line of text here
第五行文字在这里
Sixth Line of text here.
第六行文字在这里。
Seventh Line of text here.
第七行文字在这里。
Eighth Line of text here.
第八行文字在这里。
I've tried every combination of new line commands (vbcrlf, vbcr, vblf, vbnewline, etc) and every one results in the same outcome of double-spacing once I get how I need it in the vba string. I've also tried doing this concatenation right in the .Body and as a string object shown here...same result for both approaches.
我已经尝试了新行命令(vbcrlf、vbcr、vblf、vbnewline 等)的每一种组合,一旦我在 vba 字符串中得到我需要的结果,每个组合都会产生相同的双倍行距结果。我也尝试过在 .Body 中进行这种连接,并作为此处显示的字符串对象...两种方法的结果相同。
Any idea(s) on how to prevent this double-spacing from occurring? Or, if not, is there way to manipulate the line spacing to 0pt in the e-mail body when creating?
关于如何防止这种双倍间距发生的任何想法?或者,如果没有,有没有办法在创建时将电子邮件正文中的行距操纵为 0pt?
Thanks in advance for any assistance.
在此先感谢您的帮助。
回答by Erikas
I've recently sorted it out (I had different scenario, but the solution would be the same. I will try to explain why it happens:
我最近整理了它(我有不同的场景,但解决方案是相同的。我将尝试解释为什么会发生这种情况:
You are creating HTML body of you mail. Each line is presented with a following format (or at least - similar):
您正在创建邮件的 HTML 正文。每行都以以下格式呈现(或至少 - 类似):
<p><span style="color: #1f497d;">THISISSOMETEXT</span></p>
But you know what? 'p' stands for Paragraph, and by default, MS Office splits paragraphs with a large break (new line). To avoid this, use following format when creating HTML body for your email:
但是你知道吗?'p' 代表段落,默认情况下,MS Office 用大的分隔符(换行)分割段落。为避免这种情况,请在为电子邮件创建 HTML 正文时使用以下格式:
<span style="color: #1f497d;">THISISSOMETEXT</span>
And use <BR>
for new line. Example HTML body:
并<BR>
用于新线。示例 HTML 正文:
<span style="color: #1f497d;">Hello,</span>
<BR><BR>
<span style="color: #1f497d;">REPLACEMEWITHGENERATEDSENTENCE</span>
<BR><BR>
<span style="color: #1f497d;">PLEASELETMEKNOWIFYOUHAVEANYISSUES</span>
<BR><BR>
<span style="color: #1f497d;">Regards,</span><BR><BR>
But wait, there is more!You won't have signature added while using BodyHTML property instead of Body. Create new function:
但是等等,还有更多!使用 BodyHTML 属性而不是 Body 时,您不会添加签名。创建新函数:
Function getSignature() As String
signature = Environ("appdata") & "\Microsoft\Signatures\"
If Dir(signature, vbDirectory) <> vbNullString Then
signature = signature & Dir$(signature & "*.htm")
Else:
signature = ""
End If
getSignature = CreateObject("Scripting.FileSystemObject").GetFile(signature).OpenAsTextStream(1, -2).ReadAll
End Function
And at the end, use something like:
最后,使用类似的东西:
htmlBodyCode = "<span>This is HTML body</span>"
outMail.HTMLBody = "<p>" & htmlBodyCode & "</p>" & getSignature()
Hope this helps! I am currently using this set up to generate massive amount of emails from Excel in the company I am currently working.
希望这可以帮助!我目前正在使用此设置从我目前工作的公司中的 Excel 生成大量电子邮件。