【问题标题】:How do I insert a linebreak when sending an email using VBA Access使用 VBA Access 发送电子邮件时如何插入换行符
【发布时间】:2023-04-04 20:28:01
【问题描述】:

使用对象“Outlook.Application”,我使用 VBA Access 发送电子邮件。在正文中,我放了一个这样的字符串:

Email = "Random things" & Chr(13) _
& "More random things" & Chr(13) _

如果我在MsgBox 中显示字符串Email,它会正确显示,但是当我发送它时,换行符会被删除。

我试过了:

  • Chr(13)
  • vbCrLf
  • vbCr

但是所有三个都有相同的结果:

【问题讨论】:

  • 如果你将Chr(13) 和Chr(10) 结合起来(不记得确切的顺序,但我认为是这个),那应该可以解决问题! ;)
  • 它不起作用 :( 我试过:Chr(10) & Chr(13) 和 Chr(13) & Chr(10) 但没有任何改变
  • 好吧...所以还有:vbNewLine、vbVerticalTab、vbLf、vbFormFeed,也许还有vbBack
  • vbCrLf 一直为我工作,然而如果您发送纯文本电子邮件,您无法保证收件人电子邮件客户端将如何呈现邮件.他们通常会尝试“整理”他们认为包含不必要换行符的消息。在您的示例中,它们不是正确形成的句子,因此客户可能认为它们是同一个句子的一部分,因此将它们“连接”在一起。如果您需要控制布局,您需要查看使用.HTMLBody
  • @andshrew 说了什么。我建议您不要使用纯文本电子邮件并使用 HTML。这样您就可以更好地控制正文格式,并且可以使用 <br> 等标签。

标签: vba ms-access


【解决方案1】:

试试这个:

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

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

【讨论】:

    【解决方案2】:

    下面的代码在 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
    

    您可以将 HTMLBody(.BodyFormat = 2)用于格式精美的电子邮件,或将 .Body 用于纯文本电子邮件。请注意,%0D%0A 并不能在 HTMLBody 中工作,因为 Outlook 会解析它。

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 2021-02-14
      • 2011-09-28
      • 2021-02-05
      • 2021-11-21
      • 2020-05-20
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多