In loops like this dump the richtext.text to a string, then use the instr() on the string.

Call the rtf control as little as possible.

The performance boost is worth the effort of changing the code.