Examples: findandReplace method

This agent replaces all occurrences of a string in the Body item of the current or first selected document.

import lotus.domino.*;

public class JavaAgent extends AgentBase {

  public void NotesMain() {

    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();

      // (Your code goes here) 
      DocumentCollection dc = agentContext.getUnprocessedDocuments();
      Document doc = dc.getFirstDocument();
      RichTextItem body = (RichTextItem)doc.getFirstItem("Body");
      RichTextNavigator rtnav = body.createNavigator();
      if (rtnav.findFirstElement(
      RichTextItem.RTELEM_TYPE_TEXTPARAGRAPH)) {
        String foo = "Foo";
        String bar = "Bar";
        RichTextRange rtrange = body.createRange();
        int count = rtrange.findandReplace(foo, bar,
          RichTextItem.RT_FIND_CASEINSENSITIVE +
          RichTextItem.RT_REPL_ALL +
          RichTextItem.RT_REPL_PRESERVECASE);
        if (count > 0) {
          System.out.println(count + " Foos replaced by Bars");
          doc.save(true, true);
        }
        else {
          count = rtrange.findandReplace(bar, foo,
            RichTextItem.RT_FIND_CASEINSENSITIVE +
            RichTextItem.RT_REPL_ALL +
            RichTextItem.RT_REPL_PRESERVECASE);
          if (count > 0) {
            System.out.println(count + " Bars replaced by Foos");
            doc.save(true, true);
          }
          else
            System.out.println("No Foos or Bars in Body");
        }
      }
      else
        System.out.println("Body does not contain any text");

    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}