user3323922 user3323922 - 7 months ago 30
HTML Question

HTML NewLine Issue

ScreeshotI have the below code in VBA which includes newline (vbnewline / vbcrlf) . Then I am calling the variable "Clientname" in .htmlbody. But it is putting the entire string in one line eliminating the newline

'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


HTML doesn't understand/parse line feeds in the same way as VBA does. Use <BR> tag for HTML.

Change this line ClientName = ClientName & vbNewLine & un to ClientName = ClientName & "<BR>" & un

Edit as per your updated question:

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