Examples: BeginInsert method

This agent inserts text at (depending on user specification) the beginning or end of an item, or before or after the nth paragraph.

Dim session As NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim body As NotesRichTextItem
Dim rtnav As NotesRichTextNavigator
Dim beginningText As Variant, endText As Variant
Dim beforeText As Variant, afterText As Variant

Sub Initialize
  Set session = New NotesSession
  Set db = session.CurrentDatabase
  Set dc = db.UnprocessedDocuments
  If dc.Count = 0 Then
    Messagebox "No document selected",, "No doc"
    Exit Sub
  End If
  Set doc = dc.GetFirstDocument
  Set body = doc.GetFirstItem("Body")
  Set rtnav = body.CreateNavigator
  
  REM Get user command and parse
  If body.Text <> "" Then
    inp$ = Inputbox$("Beginning, End, Before n, or After n", _
    "Where do you want the new text?")
    If inp$ = "" Then Exit Sub
    inp$ = Lcase(inp$)  
    If Left(inp$, 9) = "beginning" Then
      beginningText = True
    Elseif Left(inp$, 3) = "end" Then
      endText = True
    Elseif Left(inp$, 7) = "before " Then
      nthStr$ = Right(inp$, Len(inp$) - 7)
      beforeText = True
    Elseif Left(inp$, 6) = "after " Then
      nthStr$ = Right(inp$, Len(inp$) - 6)
      afterText = True
    Else
      Messagebox inp$,, "Bad option"
      Exit Sub
    End If
    If beforeText Or afterText Then
      If Isnumeric(nthStr$) Then
        nth% = Cint(nthStr$)
      Else
        Messagebox nthStr$,, "Not numeric"
        Exit Sub
      End If
    End If
  End If
  
  REM Position navigator
  REM (not necessary for "end" or empty item)
  If beginningText Then
    Call rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH)
  Elseif beforeText Or afterText Then
    If Not rtnav.FindNthElement(RTELEM_TYPE_TEXTPARAGRAPH, _
    nth%) Then
      Messagebox nth%,, "No paragraph at this position"
      Exit Sub
    End If
  End If
  
  REM Begin insert (not necessary for "end" or empty item)
  If afterText Then
    Call body.BeginInsert(rtnav, True)
  Elseif beginningText Or beforeText Then
    Call body.BeginInsert(rtnav)
  End If
  
  REM Get text from user and append
  it$ = Inputbox$("Enter the new text", "New text")
  If it$ = "" Then Exit Sub
  If endText Or afterText Then
    Call body.AddNewLine(1)
  End If
  Call body.AppendText(it$)
  If beginningText Or beforeText Then
    Call body.AddNewLine(1)
  End If
  
  REM End insert (not necessary for "end" or empty item)
  If beginningText Or beforeText Or afterText Then
    Call body.EndInsert
  End If
  
  Call doc.Save(True, True)
End Sub