nextline vba with excel生成的电子邮件

时间:2016-01-19 17:04:03

标签: excel vba excel-vba email

通过互联网后,我仍然遇到了在Excel生成的电子邮件中插入新行的问题。

我尝试过以下几段代码:

 Email = "person@email.com"
 Subj = "Subject"
 body = "Hello Person," & vbCr & _
 "message" & vbCr & _
 "Example: " & vbCr & _
 "another example" & vbCr & _
 "another example" & vbCr & _
 "Thank you. "

 body = "Hello Person," & vbCr & _
 body = body & "message" & vbCr & _
 etc etc

我试过

 body = "Hello Person," & vbNewline & _
 body = body & "message" & vbNewline & _
 etc etc

第一个示例工作正常,但没有额外的行。第二个示例引发了不匹配错误

对这个问题有什么想法?

enter image description here 用于生成电子邮件的代码

 URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg & "        " & body
 ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus

 Private Declare Function ShellExecute Lib "shell32.dll" _
 Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
 ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As           String, _
 ByVal nShowCmd As Long) As Long

2 个答案:

答案 0 :(得分:2)

这与@Slubee的答案非常相似,但我认为它更完整,希望能帮助您找出问题所在。试试这段代码:

Option Explicit

Sub test()
    Dim email As String, _
        subj As String, _
        body As String

    email = "Foo@boo.com"
    subj = "Subject goes here"
    body = "Hello Person," & vbNewLine & _
            "message" & vbNewLine & _
            "Example:" & vbNewLine & _
            "another example" & vbNewLine & _
            "another example" & vbNewLine & _
            "Thank you. "

    MsgBox body

    body = "Hello Person," & vbCr & _
            "message" & vbCr & _
            "Example:" & vbCr & _
            "another example" & vbCr & _
            "another example" & vbCr & _
            "Thank you. "

    MsgBox body
End Sub

您会看到他们生成相同的内容(并且它与您放置的屏幕截图相匹配)。我的观点是,我认为问题在于您使用body变量而不是Excel / VBA构造字符串的方式。

以下是一些使用Outlook创建电子邮件的代码,可以提供预期的结果。如果您不使用Outlook,则必须澄清您用于生成电子邮件的程序:

Option Explicit

    Sub test()
        Dim email As String, _
            subj As String, _
            body As String

        Dim OutApp As Object, _
            OutMail As Object

        email = "Foo@boo.com"
        subj = "Subject goes here"

        body = "Hello Person," & vbCr & _
                "message" & vbCr & _
                "Example:" & vbCr & _
                "another example" & vbCr & _
                "another example" & vbCr & _
                "Thank you. "

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

        On Error Resume Next
            With OutMail
                .To = email
                .CC = ""
                .BCC = ""
                .Subject = subj
                .body = body
                .Display
            End With
        On Error GoTo 0

        Set OutMail = Nothing
        Set OutApp = Nothing
    End Sub

答案 1 :(得分:1)

使用vbNewline添加行。

"你好,#34; &安培; Vbnewline&