[Urgent] NotesRichTextRange FindAndReplace Cannot replace at the same line

I write an function to replace string in rich text:

Function RichTextReplaceSystemVariable(MailDoc As NotesDocument,contactdoc As NotesDocument)

On Error Goto ErrorTrap

Dim session As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Dim body As Variant

Dim rtnav As NotesRichTextNavigator

Dim rtrange As NotesRichTextRange

Dim searchString As String

Dim replaceString As String

Dim n As Integer

Dim i As Integer

Dim returnval As Long

Set body = MailDoc.GetFirstItem(“Body”)

n = 0

Set rtnav = body.CreateNavigator

For i=1 To 6

Set rtrange = body.CreateRange

Call rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH)

Call rtrange.SetBegin(rtnav)

Select Case i

Case 1

searchString = “[[Salute]]”

replaceString = contactdoc.Salute(0)

Case 2

searchString = “[[LastName]]”

replaceString = contactdoc.LastName(0)

Case 3

searchString = “[[FirstName]]”

replaceString = contactdoc.FirstName(0)

Case 4

searchString = “[[CompanyName]]”

replaceString = contactdoc.CompanyName(0)

Case 5

searchString = “[[Address]]”

Set db = session.CurrentDatabase

Set view = db.GetView(“($CustomerID)”)

Call view.Refresh

Set doc = view.GetDocumentByKey(contactdoc.CustomerID(0),True)

If Not doc Is Nothing Then

replaceString = “”

Forall ad In doc.GetItemValue(“Address”+contactdoc.Address(0))

If replaceString = “” Then

replaceString = ad

Else

replaceString = replaceString+Chr(10)+Chr(13)+ad

End If

End Forall

End If

Case 6

searchString = “[[PhoneNumber]]”

replaceString = Cstr(contactdoc.Phone(0))

End Select

While rtrange.FindAndReplace(searchString, replaceString)

n = n + 1

Set rtrange = body.CreateRange ’ NEW

Call rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH) ’ NEW

Call rtrange.SetBegin(rtnav) ’ NEW

Call body.Update

Wend

If n > 0 Then

Call body.Compact

End If

Next

Exit Function

ErrorTrap:

Print “Error Occured during running”

Print "tgSLCustomer ReichTextReplaceSystemVariable " + Error$ " occured on line " & Erl & " of the thread "& Getthreadinfo(1)

Resume Next

End Function

I find that if the line contains

Dear [[Salute]] [[FirstName]] [[LastName]] ,

the result becomes:

Dear [[Salute]] [[FirstName]] Mr XFirst YLast,

what is the problem with my code?

Subject: [Urgent] NotesRichTextRange FindAndReplace Cannot replace at the same line

Hi,

Im not very sure about ur code. But u have used chr(10) and chr(13) in the replacestring like this : replaceString = replaceString+Chr(10)+Chr(13)+ad

Here chr(10) and chr(13) will introduce newlines. So it will replace with new lines. Remove chr and try it out.