HTML NewLine问题

时间:2016-08-19 16:36:01

标签: html vba

Screeshot我在VBA中有以下代码,其中包含换行符(vbnewline / vbcrlf)。然后我调用变量" Clientname"在.htmlbody。但它将整个字符串放在一行中,消除了换行符

  'Variable "un" will be assigned a value using a "For loop" above this code

 Reading Text file: 

Set oFS = oFSO.OpenTextFile("c:\test.txt")
TxtPro = oFS.ReadAll

 If Not (InStr(ClientName, un)) > 0 Then
   ClientName = ClientName & vbNewLine & un
 End If

with objmail
.bodyformat = olformatHTML
.htmlbody = "<HTML><BODY> " & clientname  & _
     "<Br> Your File is given below <br> " & txtpro & "</body></html>"
end with

1 个答案:

答案 0 :(得分:1)

HTML不像VBA那样理解/解析换行符。使用<BR>标记表示HTML。

将此行ClientName = ClientName & vbNewLine & un更改为ClientName = ClientName & "<BR>" & un

根据您更新的问题进行修改:

Sub test()

    Dim oFSO         As New Scripting.FileSystemObject
    Dim oFS          As Object

    Dim txtPro      As String
    Dim strHTML     As String
    Dim clientName  As String

    Set oFS = oFSO.OpenTextFile("C:\temp\test.txt")
    txtPro = oFS.ReadAll()

    '/ This will replace linefeeds with <BR> to render line breaks in HTML
    txtPro = Replace(txtPro, vbCrLf, "<BR>")

    strHTML = "<HTML><BODY> " & clientName & _
     "<Br> Your File is given below <br> " & txtPro & "</body></html>"

End Sub