如何使用VBA Access

时间:2015-06-11 08:05:45

标签: vba access-vba

使用对象" Outlook.Application",我使用VBA Access发送电子邮件。在身体里,我放了一个这样的字符串:

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

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

我试过了:

  • Chr(13)
  • vbCrLf
  • vbCr

但这三个都有相同的结果:

enter image description here

2 个答案:

答案 0 :(得分:4)

试试这个:

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

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

答案 1 :(得分:0)

下面的代码显示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对其进行了解析。