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:
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!
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!
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.
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).
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)