修复VBA调用Outlook发送邮件时长文本自动产生多余换行的问题
问题根源
你遇到的长文本自动换行是Outlook纯文本邮件的默认规则:默认会为超过78字符的纯文本行自动插入强制换行符,所以第2条内容较长时会被自动拆行。
解决方案
推荐改用HTML格式编写邮件正文,既可以避免自动换行问题,也能自定义排版格式,修改后的完整代码如下:
Private Sub CommandButton1_Click() 'Updated by Extendoffice 2017/9/14 Dim xOutApp As Object Dim xOutMail As Object Dim xMailBody As String On Error Resume Next Set xOutApp = CreateObject("Outlook.Application") Set xOutMail = xOutApp.CreateItem(0) ' 改用HTML格式编写正文 xMailBody = "<p><strong>We need all of the following to get the control points to set up the Point Files.</strong></p>" & _ "<ol>" & _ "<li>Civil CAD drawing with all project grids placed in true coordinates.</li>" & _ "<li>CSV or Text file with Northing, Easting and Elevation coordinates of all marked and confirmed site control locations.</li>" & _ "<li>Job Site: </li>" & _ "<li>Requester Name: </li>" & _ "<li>Brief detail of reason for training need(s): </li>" & _ "</ol>" On Error Resume Next With xOutMail .To = "jsmith@test.com" .CC = "" .BCC = "" .Subject = "INFORMATION NEEDED TO CREATE SITE AND BUILDING CONTROL" ' 替换原来的.Body为.HTMLBody .HTMLBody = xMailBody .Display 'or use .Send End With On Error GoTo 0 Set xOutMail = Nothing Set xOutApp = Nothing End Sub
如果需要坚持使用纯文本格式,可以修改Outlook的纯文本自动换行设置,也可以在代码中把长文本按每行76字符左右手动拆分拼接,规避自动触发默认换行规则,但适配性远不如HTML格式方案稳定。
内容的提问来源于stack exchange,提问作者Mahshid Rosario
相关产品推荐
相关产品推荐

