vba - nextline vba 与 excel 生成电子邮件

标签 vba excel 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

最佳答案

这与 @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

关于vba - nextline vba 与 excel 生成电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34882744/

相关文章:

javascript - 实时时间表编辑和保存

excel - 只为一组中的 5 个最高值查找 AVERAGEIFS

python - 发送电子邮件时 Django 线程是否被阻塞?

PHP 电子邮件表单通过时单词之间没有空格,也没有特殊字符

php - 使用 PHP Swiftmailer 时如何解决错误 554 5.5.1(无有效收件人)?

vba - 我有一个宏来刷新工作簿中的所有数据透视表,我需要添加代码来删除(空白)

excel - VBA Selenium FindElementByXPath 找不到元素

vba - If-then/为单元格引用中定义的日期范围选择案例

sql - 如何在 where 子句中使用我的别名?

excel - 如何将基于多个单元格条件的值复制到另一个工作表