vba - 通过从带有换行符 : VBA 的单元格值创建消息来在电子邮件中新建行

标签 vba excel email

我遇到了与 VBA 中的邮件相关的问题。

例如。在单元格 A1 中我有:

Test test test

test


test test

当我从单元格 A1 生成邮件时,字符串显示时没有换行符,我的意思是

Test test test test test test. 

我尝试使用 "\r\n" 中的替换至"<br/>"但这不起作用。

Content = Replace(Content , "\r\n", "<br/>")

谁能帮我解决这个问题吗?

最佳答案

关键是替换Excel的换行符Chr(10)使用 HTML 换行符 <br>

电子邮件代码:

Sub mail()
    Dim OutApp As Object, OutMail As Object, strbody As String

    ' Get string from cell, replace Chr(10) (the cell new line) with a html <br>
    strbody = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
    strbody = Replace(strbody, Chr(10), "<br>")

    ' Create email and add string as HTMLBODY item
    On Error Resume Next
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .to = ""
        .CC = ""
        .Subject = "Subject here"
        .htmlbody = strbody
        .Display
    End With
    On Error GoTo 0
End Sub

输出:

test

关于vba - 通过从带有换行符 : VBA 的单元格值创建消息来在电子邮件中新建行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695424/

相关文章:

vba - 总和最大但小于特定值的子集

vba - 如何等到 ActiveWorkbook.RefreshAll 完成后再执行更多代码

ruby-on-rails - 配置设备以发送电子邮件

javascript - 将输入类型文件 (HTML) 转换为 JavaScript 并使用 PHPmailer 发送电子邮件

vba - 使用 Trim 时出现运行时错误 13

html - Excel VB 在网页上搜索文本并在同一元素中复制信息

windows - 为什么从命令行启动时 Excel 在退出时抛出异常?

excel - 如何只允许某个字符在一行单元格中出现一次

vba - 运行 Excel 宏时,截图工具无法启动截图?

php - 我怎样才能确保我不会从 php 测试站点向客户发送任何电子邮件