Sub ReplaceToken(Byval Token As String, Byval Replacement As String, _
RTBody As NotesRichTextItem, NewLineChar As String)
Dim rtNav As NotesRichTextNavigator
Dim rtNavEnd As NotesRichTextNavigator
Dim rtRange As NotesRichTextRange
Dim linelength As Integer
Dim oneline As String
Set rtNav = RTBody.CreateNavigator
Set rtRange = RTBody.CreateRange
If rtNav.FindFirstString(Token) Then
'Mark the original token so you can delete it later
Call rtRange.SetBegin(rtNav)
Set rtNavEnd = rtNav.Clone
Call rtNavEnd.SetCharOffset(Len(Token))
Call rtRange.SetEnd(rtNavEnd)
'Preserve the existing style. This gets a little convoluted.
' 1) The style is associated with a NotesRichTextRange.
' 2) You have to BeginInsert before you can AppendStyle.
Call RTBody.BeginInsert(rtNav)
Call RTBody.AppendStyle(rtRange.Style)
'Break up multiple lines (assumes a single character delimiter)
linelength = Instr(1, Replacement, NewLineChar)
Do Until linelength = 0
oneline = Left$(Replacement, linelength - 1)
Call RTBody.AppendText(oneline)
Call RTBody.AddNewline(1)
Replacement = Mid$(Replacement, linelength + 1)
linelength = Instr(1, Replacement, NewLineChar)
Loop
'Add the final line of replacement text, or the only line if there are
' not multiple lines of replacement text
Call RTBody.AppendText(Replacement)
Call RTBody.EndInsert
'Delete the original token.
Call rtRange.Remove
End If
Set rtRange = Nothing
Set rtNav = Nothing
Set rtNavEnd = Nothing
End Sub
provided by Julian Robichaux at nsftools.com.
See, I do write code sometimes! :-)
SnTT , show-n-tell thursday
Is there some reason you didn't use the FindandReplace method of the NotesRichTextRange class? I have used it myself and it's kept the formatting and style of what I replaced. Have you had different experiences?
ReplyDeleteSean---
My biggest problem was FindAndReplace won't include carriage returns. I also had mixed results with getting it to retain the style of the text being replaced. If the token was in with other text it would retain it, but if the token was on a line by itself it wouldn't. I figure it has something to do with paragraph marks, but I never could get it to work consistently.
ReplyDeleteNice code, but when trying to call the sub multiple times on the same body field (e.g. for tokens [FirstName], [LastName], [Email], ...),
ReplyDeleteIt does not seem to work correctly, leaving sometimes the token behind.
Any ideas ?
Theo, I do exactly the same thing. Send me an e-mail with the RichText you're working with and I'll take a look at it.
ReplyDeletegreat stuff, works great, and using loops on repeatable tokens was also good.
ReplyDeleteworks great
ReplyDeleteHi Charles,
ReplyDeleteI was trying to use this subroutine, what do I pass it for the newline value?
thanks in advance
I used this specifically with an [ADDRESS] token. I would call it like this:
ReplyDeleteCall ReplaceToken("[ADDRESS]", "123 Oak Lane|Atlanta, GA|30329", notesBody, "|"
This would break the address into multiple lines. If you aren't doing this you can pass in any character that doesn't exist in your Rich Text field.