Excel VBA文本框保持换行

Excel VBA文本框保持换行,excel,vba,Excel,Vba,我正在使用Excel中的VBA创建电子邮件。对于电子邮件的正文,我采用excel表格中文本框的值。我在文本框中启用了多行,并将文本放在第一行,将文本放在下一行,但当我生成电子邮件时,它会将两行文本放在电子邮件中的同一行 我需要知道如何在文本框中保留换行符 Sub Test1() Dim OutApp As Object Dim OutMail As Object Dim cell As Range Dim SigString As String Dim Signature As String

我正在使用Excel中的VBA创建电子邮件。对于电子邮件的正文,我采用excel表格中文本框的值。我在文本框中启用了多行,并将文本放在第一行,将文本放在下一行,但当我生成电子邮件时,它会将两行文本放在电子邮件中的同一行

我需要知道如何在文本框中保留换行符

Sub Test1()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim SigString As String
Dim Signature As String

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
       LCase(Cells(cell.Row, "C").Value) = "yes" Then

        Set OutMail = OutApp.CreateItem(0)

        SigString = Environ("appdata") & _
            "\Microsoft\Signatures\Default.htm"

        If Dir(SigString) <> "" Then
            Signature = GetBoiler(SigString)
        Else
            Signature = ""
        End If

        On Error Resume Next
        With OutMail
            .Display
            .To = cell.Value
            .Subject = "Reminder"
            .HTMLBody = "<p>Dear " & Cells(cell.Row, "A").Value & ",</p>" _
                  & "<br><br>" & ActiveSheet.TextBox1.Value & _
                  .HTMLBody               
            .Attachments.Add ("")
            .Display
        End With
        On Error GoTo 0
        Set OutMail = Nothing
    End If
Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

Function GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function
子测试1()
Dim OutApp作为对象
将邮件变暗为对象
暗淡单元格作为范围
字符串作为字符串
作为字符串的数字签名
Application.ScreenUpdating=False
Set-OutApp=CreateObject(“Outlook.Application”)
关于错误转到清理
对于列(“B”).Cells.SpecialCells(xlCellTypeConstants)中的每个单元格
如果单元格值像“*@*。?*”和_
LCase(Cells(cell.Row,“C”).Value)=“yes”然后
Set-OutMail=OutApp.CreateItem(0)
SigString=Environ(“appdata”)&_
“\Microsoft\Signatures\Default.htm”
如果Dir(SigString)“,则
Signature=GetBoiler(SigString)
其他的
Signature=“”
如果结束
出错时继续下一步
发邮件
.展示
.To=单元格.Value
.Subject=“提醒”
.HTMLBody=“亲爱的”单元格(cell.Row,“A”).Value&“,

”_ &“

”&ActiveSheet.TextBox1.Value&_ .HTMLBody .Attachments.Add(“”) .展示 以 错误转到0 发送邮件=无 如果结束 下一个细胞 清理: 设置应用程序=无 Application.ScreenUpdating=True 端接头 函数GetBoiler(ByVal sFile作为字符串)作为字符串 “迪克·库斯利卡 作为对象的Dim fso 将T作为对象 设置fso=CreateObject(“Scripting.FileSystemObject”) 设置ts=fso.GetFile(sFile).OpenAsTextStream(1,-2) GetBoiler=ts.readall 关闭 端函数

以下是TextBox1属性

您可以
在此处拆分
字符串以指定行数


.HTMLBody=“亲爱的”单元格(cell.Row,“A”).Value&“,

”_ &“

”_ &拆分(ActiveSheet.TextBox1.Value,“.”(0)和“.”_ &“
”_ &拆分(ActiveSheet.TextBox1.Value,“.”(1)和“.”_ &.HTMLBody
您可以用HTML格式的换行符替换换行符,
vbNewLine
,如下所示:

With outMail
    .display
    .To = Cell.Value
    .subject = "Reminder"
    .HTMLBody = "<p>Dear " & Cells(Cell.row, "A").Value & ",</p>" _
          & "<br><br>" & Replace(ActiveSheet.TextBox1.Value, vbNewLine, "<br>") & _
          .HTMLBody
    .Attachments.Add ("")
    .display
End With
与outMail
.展示
.To=单元格.Value
.subject=“提醒”
.HTMLBody=“亲爱的”单元格(Cell.row,“A”).Value&“,

”_ &“

”&Replace(ActiveSheet.TextBox1.Value,vbNewLine,”
”&_ .HTMLBody .Attachments.Add(“”) .展示 以
您在excel中的文本是否已包装,或者是否存在有效的换行符(
Chr(10)
)。如果您的文本刚刚包装,格式将不会继续,因为它将相应地调整大小。对于emailI添加的文本框及其属性图片,您可能需要手动拆分字符串。
With outMail
    .display
    .To = Cell.Value
    .subject = "Reminder"
    .HTMLBody = "<p>Dear " & Cells(Cell.row, "A").Value & ",</p>" _
          & "<br><br>" & Replace(ActiveSheet.TextBox1.Value, vbNewLine, "<br>") & _
          .HTMLBody
    .Attachments.Add ("")
    .display
End With