user2469010
user2469010

Reputation: 31

DIM text as string variable is not showing new line returns or spaces when using vbnewline or chr(10)

I am using a script that emails people from an excel spreadsheet through outlook in VBA. I had to wrestle a lot to get the signatures working in the emails. Part of what I had to do to get it was move the body of the email into a text variable. and change the body type to html for handling the signature. The signatures now work but all the indentation and new line breaks in the text no longer display.

I have tried using vbnewline and chr(10) but neither seem to register now that the text is in a string variable called text

Sub Test1()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim text As String

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

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

            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
            .Display
                .To = cell.Value
                .Subject = "PSJH SAR / Net IQ Retirement Organization Setup (Follow Up)"
                 text = "Hello " & Cells(cell.Row, "A").Value _
                      & vbNewLine & vbNewLine & _
                        "this is XYZ from the XYZ and I just left you a voicemail message for you. " & _
                        "We are reaching out to you because you’ve been identified in the XYZ system as someone who manages XYZ " & _
                        "The XYZ form, which you have been using, will be retired after XYZ and be replaced with a new process and system. " & _
                        "If you XYZ for your organization, this means you will be directly impacted. " & _
                        "We need to collect your information to set you up in our new system and ensure there is no interruption moving forwards. " & _
                        "We will reach out again and if you can please provide the following information below: " & _
                        vbNewLine & vbNewLine & _
                        "Best email to contact you: " & _
                        vbNewLine & _
                        "Best phone number to reach you: " & _
                        vbNewLine & _
                        "Best time of day to schedule our next call: " & _
                        vbNewLine & vbNewLine & _
                        "If you have any questions or concerns, please don’t hesitate to reach out directly to me at XYZ " & _
                        vbNewLine & vbNewLine & _
                        "Thank you, "
                .HTMLbody = text & vbNewLine & .HTMLbody



                .Send  
            End With
            On Error GoTo 0
            Set OutMail = Nothing
        End If
    Next cell

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

Signature displays and code works but text string with email body is not formatted according to line break values

Upvotes: 0

Views: 742

Answers (2)

braX
braX

Reputation: 11755

When sending an email using HTMLBody you need to use HTML instead. So that means instead of using the vbNewLine constant, use the text <br> instead.

<br> is a a line break.

text = "Hello<br><br>World<br><br><br>Like this."

If you are planning on using more than one space in a row, you will need to use a non-breaking space (character entity), like this.

text = "Hello&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;World."

Upvotes: 1

user2469010
user2469010

Reputation: 31

Replaced all instances of vbNewLine with "
"

Upvotes: 0

Related Questions