使用VBA Access发送电子邮件时如何插入换行符

使用VBA Access发送电子邮件时如何插入换行符,vba,ms-access,Vba,Ms Access,使用对象“Outlook.Application”,我使用VBA Access发送电子邮件。在身体里,我放了一根这样的线: Email = "Random things" & Chr(13) _ & "More random things" & Chr(13) _ 如果我在MsgBox中显示字符串Email,它将正确显示,但当我发送它时,换行符将被删除 我试过: Chr(13) vbCrLf vbCr 但这三种方法都有相同的结果: 试试这个: Sub Outlook

使用对象“Outlook.Application”,我使用VBA Access发送电子邮件。在身体里,我放了一根这样的线:

Email = "Random things" & Chr(13) _
& "More random things" & Chr(13) _
如果我在
MsgBox
中显示字符串
Email
,它将正确显示,但当我发送它时,换行符将被删除

我试过:

  • Chr(13)
  • vbCrLf
  • vbCr
但这三种方法都有相同的结果:

试试这个:

Sub OutlookEmail()
Dim AppOutlook As Outlook.Application
    Set AppOutlook = CreateObject("Outlook.application")

Dim Mail As MailItem
    Set Mail = AppOutlook.CreateItem(olMailItem)

Dim Email As String
    Email  = "Random things" & vbNewLine _
             & "More random things" & vbNewLine

'Generate Email

Mail.Subject = "Test Subject"
Mail.To = "Test@test.com"

Mail.Body = Email

Mail.Display

Set Mail = Nothing
Set AppOutlook = Nothing

End Sub

经测试,我自己似乎在电脑上正常工作。

下面的代码将在Outlook中显示电子邮件。要发送,请将.Display更改为.send

Sub SendDisplayEmail(strEmailFrom As String, strEmailTo As String, strEmailCC As String, strEmailBCC As String, strSubject As String)
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0) ' olMailItem

    Debug.Print ("From: " & strEmailFrom & ", To: " & strEmailTo & ", cc: " & strEmailCC & ", bcc: " & strEmailBCC & ", file: " & xFile)

    On Error Resume Next

    OutMail
    With OutMail
        .to = strEmailTo
        .CC = strEmailCC
        .BCC = strEmailBCC
        .Subject = strSubject
        '.Body = "Random things" _
        '    & vbCrLf & vbCrLf & "More random things." _
        .BodyFormat = 2 ' olFormatHTML
        .HTMLBody = "<html>Random things<br>More random things.</html>"
        '.Body = strBody
        '.Save
        .Display
        '.Send   'or use .Display
    End With

    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
子发送显示电子邮件(strEmailFrom作为字符串,strEmailTo作为字符串,strEmailCC作为字符串,strEmailBCC作为字符串,strSubject作为字符串)
Dim OutApp作为对象
将邮件变暗为对象
应用
.ScreenUpdate=False
.EnableEvents=False
以
Set-OutApp=CreateObject(“Outlook.Application”)
设置OutMail=OutApp.CreateItem(0)'olMailItem
Debug.Print(“From:&strEmailFrom&”,To:&strEmailTo&”,cc:&strEmailCC&”,bcc:&strEmailBCC&”,文件:&xFile)
出错时继续下一步
寄出
发邮件
.to=strEmailTo
.CC=strEmailCC
.BCC=strEmailBCC
.Subject=strSubject
“.Body=”随机事物“_
“&vbCrLf&vbCrLf&”更多随机事件。“_
.BodyFormat=2'olFormatHTML
.HTMLBody=“随机事件
更多随机事件。” “.Body=strBody "救命! .展示 “.Send”或使用.Display 以 错误转到0 发送邮件=无 设置应用程序=无 应用 .ScreenUpdate=True .EnableEvents=True 以 端接头

您可以使用HTMLBody(带.BodyFormat=2)处理格式良好的电子邮件,也可以使用.Body处理纯文本电子邮件。请注意,不要在HTMLBody中工作,因为Outlook会解析它

如果您将
Chr(13)
Chr(10)
组合在一起(不一定记得顺序,但我认为是这一个),应该就可以了!;)它不起作用:(我试过:Chr(10)&Chr(13)和Chr(13)&Chr(10)但是一切都没有改变…所以仍然有:
vbNewLine
vbVerticalTab
vbLf
vbFormFeed
,也许
vbBack
vbCrLf
一直对我有效,但是如果你发送的是纯文本电子邮件,你不能保证收件人的电子邮件客户端将如何呈现邮件。通常他们试图“整理”他们认为包含不必要的换行符的消息。以你的例子来说,它们不是正确的句子,因此客户可能认为它们是同一个句子的一部分,因此将它们“连接”在一起。如果你需要控制布局,你需要使用
.HTMLBody
看看@andshruw说了什么。我会的ld建议您不要使用纯文本电子邮件和HTML。这样您可以更好地控制正文格式,例如,您可以使用

标记。