Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extra line breaks in e-mail when string is over 120 characters

Status
Not open for further replies.

BV874

Technical User
Nov 23, 2009
33
US
Hi,
I have the following code that creates an e-mail from a query if multiple parameters on a form are met. When the email is created the information from the recordset is listed line by line. If the EmailText string is over 120 characters it creates an extra line break. I have tried vbnewline, vblf, vbcrlf when opening the recordset but they all produce the same result. Any help would be greatly appreciated. Office 2007 Professional, Office 2010 64-bit, saving as mdb for users with 2003 runtime.

Thanks.

Code:
Private Sub CreateEmail2()
On Error GoTo ErrorHandler

Const olMailItem    As Long = 0
Dim olApp           As Object
Dim objEmail        As Object
Dim db              As DAO.Database
Dim qdf             As DAO.QueryDef
Dim qdf2            As DAO.QueryDef
Dim prm             As DAO.Parameter
Dim prm2            As DAO.Parameter
Dim RS              As DAO.Recordset
Dim RS2             As DAO.Recordset
Dim SESSION_ID      As Long
Dim EmailText       As String
Dim ESignature      As String

Set db = CurrentDb()

Set qdf = db.QueryDefs("qryEmailText")
    For Each prm In qdf.Parameters
        prm.Value = Eval(prm.Name)
    Next prm
    
Set RS = qdf.OpenRecordset()
        Do While Not RS.EOF
            EmailText = EmailText & Space(10) & RS.Fields("ReferralText") & vbCrLf
        RS.MoveNext
    Loop
    
Set qdf2 = db.QueryDefs("qryEmailSignature")
    For Each prm2 In qdf2.Parameters
        prm2.Value = Eval(prm.Name)
    Next prm2
    
Set RS2 = qdf2.OpenRecordset()
    If RS2.RecordCount = 0 Then
        MsgBox "Your User Id is not listed in this database. Please contact the help desk to be added.", vbInformation + vbOKOnly, "MISSING INFORMATION"
    Else
        Do While Not RS2.EOF
            ESignature = ESignature & RS2.Fields("Signature") & vbCrLf
            RS2.MoveNext
        Loop
    End If
    
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
    If olApp Is Nothing Then
        Set olApp = CreateObject("Outlook.Application")
    End If

Set objEmail = olApp.CreateItem(olMailItem)

With objEmail
    .To = Me.EMAIL
    .Subject = "Additional Resources"
    .Body = "Dear " & StrConv(Me.COMBINED_NAME, vbProperCase) & "," & vbNewLine & "Here is a list of the additional resources that we spoke about." & vbNewLine & vbCrLf & EmailText & vbNewLine & _
    "Please feel free to contact me if you need any further assistance." & vbNewLine & vbCrLf & "Regards," & vbNewLine & vbCrLf & ESignature
'    .send  'this command automatically sends the e-mail without viewing it.
    .display
End With

EndSub:
    RS.Close: Set RS = Nothing: Set prm = Nothing: Set qdf = Nothing: RS2.Close: Set RS2 = Nothing: Set prm2 = Nothing: Set qdf2 = Nothing
    db.Close: Set db = Nothing

Exit_CreateEmail:
    Exit Sub

ErrorHandler:
    MsgBox "Error Number" & Err.Number & ", " & Err.Description, vbOKOnly, "Error"
    Resume Exit_CreateEmail:

End Sub
 
Not sure if I get your question properly.
You mean you tried vblf, vbnewline etc. but you DON'T get a line break where you want it?

==>have you tried "<br/>" yet?

Depends on the format of your e-mail of course.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Hi MakeItSo,
I do get the line breaks, but there is an extra break that appears in the e-mail on long text. When I select it to manually delete it is not there. I think it may be a 1.5 line spacing issue, but it only appears on the long text. See below for an example:

Dear Test Account,
Here is a list of the additional resources that we spoke about.
Government Programs: Food Stamps, (800) 221-5689
Government Programs: General website for a variety of government benefits, 1-800-FED-INFO

Government Programs: General website for a variety of government benefits,
Government Programs: Housing and Urban Development (HUD),
Please feel free to contact me if you need any further assistance.
Regards,
 
Simply setting

objEmail.BodyFormat = olFormatRichText

should fix this
 
Also in
=>Options=>E-Mail
scroll down to "message format"
==>automatic text wrap at xxx characters
Adapt the number there.

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
olFormatRichText and the Outlook option did not work. My longest line is 164 characters. I'm going to try .htmlbody instead of .body to see if works better in that format. Thanks.
 
>olFormatRichText ... [] ... did not work

Odd. Works here ... at what point are you setting olFormatRichtext? You need to do it before you set the Body
 
I switched to .htmlbody and used "<br>" instead of the vb line breaks and it worked. Thank you both for your assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top