RichText, FindAndReplace and Carriage Returns

I’m using the FindAndReplace method of NotesRichTextRange to pick out bits of text in a Rich Text field and replace them with others. This works fine, however I’d like to be able to find a specific string, eg:

<

>

and replace it with several lines of text, eg:

detail1

detail2

detail3

I can’t seem to be able to produce the carriage returns/line breaks in my ReplaceWith text though, so it always comes out looking like this:

detail1detail2detail3

I’ve tried constructing the string in the following ways:

myStr$ = “detail1” & Chr(10) & “detail2” & Chr(10) & “detail3”

myStr$ = “detail1” & Chr(13) & “detail2” & Chr(13) & “detail3”

myStr$ = “detail1” & Chr(10) & Chr(13) & “detail2” & Chr(10) & Chr(13) & “detail3”

eval = Evaluate(“@Newline”)

myStr$ = “detail1” & eval(0) & "detail2"eval(0) & “detail3”

But none of them produce line breaks in the text.

The purpose of this code is to provide the user with the ability to set up e-mail ‘templates’, where they can specify placeholders for information which can be pulled from customer records when they want to run the mail merge. The “Body” is copied from the mail merge template document to the e-mail document, then I use FindAndReplace to replace the placeholder strings with the relevant information from the customer record.

If anyone has any ideas on how I can get the line breaks to appear, I’d be really grateful!

Thanks,

Emily.

Subject: RichText, FindAndReplace and Carriage Returns

How about using the AddNewLine method of NotesRichTextItem class ?

Subject: RE: RichText, FindAndReplace and Carriage Returns

Hi Ian - thanks for the quick response. It’s because I’m using FindAndReplace to pick out specific strings and replace them with others. The method doesn’t allow for any kind of RichText manipulation, i.e. AddNewLine.

I feel like there could be some way of using the NotesRichText classes to locate the string, then remove it and start inserting text at that point, maybe using NotesRichTextNavigator.FindFirstString. Being the end of the day, my brain’s a bit fried though so I’ll take a fresh look at it in the morning and post my results if I get any!

Emily.

Subject: RichText, FindAndReplace and Carriage Returns

Try using Chr(0), as that is really the newline used in rich text. The Chr(0) is translated to Chr(10) and Chr(13) based on platforms, since Windows and Unix use different conventions. I don’t know whether the FindAndReplace method will accept the Chr(0), but it should.

Subject: RE: RichText, FindAndReplace and Carriage Returns

Thanks for the suggestion, Ben. I hadn’t thought of trying Chr(0). Unfortunately, this doesn’t seem to work when used with FindAndReplace - you just end up with the text which occurs before the first Chr(0) and nothing else.

I’ll persevere with the RichText classes for now.

Emily.

Subject: RichText, FindAndReplace and Carriage Returns - My Solution

Ok - for anyone else that needs to replace a particular item of text in a rich text item with several lines of text, here is my solution. It’s probably not the best solution or the most elegant, but it seems to work! The basic principle is this: find the text you want to replace, set the insertion point just before it, enter the text you want to replace it with, then use FindAndReplace to replace your text with a space (FindAndReplace doesn’t seem to want to replace with a Null string “” or Chr(0).

dim memo as NotesDocument

dim memoBody as NotesRichTextItem

…

Dim rtRange As NotesRichTextRange

Set rtRange = memoBody.CreateRange

Dim rtNav As NotesRichTextNavigator

Set rtNav = memoBody.CreateNavigator

Dim foundStr As Boolean

foundStr = rtNav.FindFirstString(<>, RT_FIND_CASEINSENSITIVE)

Dim repRange As NotesRichTextRange

Do While foundStr

Set repRange = Nothing

Set repRange = memoBody.CreateRange

repRange.SetBegin(rtNav)

repRange.SetEnd(rtNav)



Call memoBody.BeginInsert(repRange, False)

Call memoBody.AppendText("Replacement text line 1")

Call memoBody.AddNewline(2)

Call memoBody.AppendText("Replacement text line 2")

Call memoBody.EndInsert



Call rtRange.FindAndReplace(<<textToReplace>>, " ", RT_FIND_CASEINSENSITIVE)	



'reset rtNav and use findfirststring because trying to use findnextstring wasn't working

Set rtNav = memoBody.CreateNavigator

foundStr = rtNav.FindFirstString(<<textToReplace>>, RT_FIND_CASEINSENSITIVE)

Loop