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