API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Find and Replace In LotusScript
A few weeks ago I was playing around with the find and replace method in the NotesRichTextRage method. It's really slick for replacing text in a rich text field. It's actually pretty easy to use. Assuming you have a rich text field in a NotesRichTextItem object, here's some simple code that will do a find/replace:

Dim range As NotesRichTextRange
Dim count As Integer
Set range = body.CreateRange ' body is a NotesRichTextItem variable
count = range.FindandReplace("text to find", "text to replace", 16) ' 16 means "replace all occurrences"
' count is the number of times it was replaced

You can refer to the Notes Help for more details - it's really easy to use. But if the text you want to replace has multiple lines, Notes will strip out the multiple lines. Even if you try something like this:
Dim range As NotesRichTextRange
Dim count As Integer
Set range = body.CreateRange ' body is a NotesRichTextItem variable
count = range.FindandReplace("text to find", |text
to
replace|, 16) ' 16 means "replace all occurrences"
' count is the number of times it was replaced

The text after the replacement doesn't have the new line characters. So this tip tells you how to replace with multiple lines. To do this, first make sure that your "replace" value is in an array.

Set range = body.CreateRange
Set nav = body.CreateNavigator ' nav is a NotesRichTextNavigator object
While nav.FindFirstString("text to find")
    Call range.SetBegin(nav)
    Call range.SetEnd(nav)
    Call range.Remove
    Call body.BeginInsert(nav)
    For x = 0 To Ubound(valuesToReplace) ' Array containing values to replace
        Call body.AppendText(valuesToReplace(x))
        If x <> Ubound(valuesToReplace) Then Call body.AddNewline(1)
    Next
    Call body.EndInsert
    Call body.Compact
    Call body.Update
    Set range = body.CreateRange
    Set nav = body.CreateNavigator
Wend

Note how this works. We find the string of text, define some bounds around it, then remove it. Then we insert the values to replace one at a time and put a new line character between them. The body is compacted and updated, and the range and navigator is refreshed. Then we find the next occurrence and continue the process.

Now comes the fun part. If you have a Notes version earlier than 6.0.3, the code above will crash Notes. The actual crash happens on the "range.SetBegin" after doing the "nav.FindFirstString". Lotus identified the problem and fixed it in Notes 6.0.3, so to get around that I do a check for session.NotesBuildVersion being greater than or equal to 194. If it's 194 or higher, then I can run through the second block of code to do a find/replace with mutliple lines. If it's less than 194, the first block still works (doesn't error or crash) but you don't get quite the expected results.