Examples: Working with text in LotusScript® classes

  1. This agent gets the text in a rich text item a paragraph at a time.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim dc As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim body As NotesRichTextItem
      Dim rtnav As NotesRichTextNavigator
      Dim rtrange As NotesRichTextRange
      Set db = session.CurrentDatabase
      Set dc = db.UnprocessedDocuments
      Set doc = dc.GetFirstDocument
      Set body = doc.GetFirstItem("Body")
      REM Find paragraphs in Body item
      Set rtnav = body.CreateNavigator
      If rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH) Then
        Set rtrange = body.CreateRange
        count% = 0
        Do
          count% = count% + 1
          msg$ = ""
          REM Set range for paragraph and display it
          Call rtrange.SetBegin(rtnav)
          msg$ = rtrange.TextParagraph
          Messagebox msg$,, "Paragraph " & count%
        Loop While rtnav.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)
      Else
        Messagebox "No text in Body",, "No text"
      End If
    End Sub
  2. This agent gets the paragraphs in a rich text item, and the runs within each paragraph.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim dc As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim body As NotesRichTextItem
      Dim rtnav As NotesRichTextNavigator
      Dim rtnav2 As NotesRichTextNavigator
      Dim rtrange As NotesRichTextRange
      Dim rtrange2 As NotesRichTextRange
      Set db = session.CurrentDatabase
      Set dc = db.UnprocessedDocuments
      Set doc = dc.GetFirstDocument
      Set body = doc.GetFirstItem("Body")
      REM Find paragraphs in Body item
      Set rtnav = body.CreateNavigator
      If rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH) Then
        Set rtrange = body.CreateRange
        Set rtnav2 = body.CreateNavigator
        Set rtrange2 = body.CreateRange
        count% = 0
        Do
          count% = count% + 1
          REM Set range for paragraph
          Call rtrange.SetBegin(rtnav)
          Call rtrange.SetEnd(rtnav)
          REM Create navigator for paragraph
          Set rtnav2 = rtrange.Navigator
          REM Find text runs in paragraph
          If rtnav2.FindFirstElement(RTELEM_TYPE_TEXTRUN) Then
            count2% = 0
            msg$ = ""
            Do
              count2% = count2% + 1
              REM Set range for text run
              Call rtrange2.SetBegin(rtnav2)
              REM Print text of run
              msg$ = rtrange2.TextRun
              Messagebox msg$,, _
              "Paragraph " & count% & ", run " & count2%
            Loop While rtnav2.FindNextElement(RTELEM_TYPE_TEXTRUN)
          End If
        Loop While rtnav.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)
      Else
        Messagebox "No text in Body",, "No text"
      End If
    End Sub
  3. This agent creates a rich text item and populates it with two paragraphs of text.
    Dim session As NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim rti As NotesRichTextItem
    
    Sub Initialize
      Set session = New NotesSession
      Set db = session.CurrentDatabase
      Set doc = db.CreateDocument
      Call doc.ReplaceItemValue("Form", "Main Topic")
      Call doc.ReplaceItemValue("Subject", "CreateRichTextItem")
      Set rti = doc.CreateRichTextItem("Body")
      Call rti.AppendText("First paragraph.")
      Call rti.AddNewLine(2)
      Call rti.AppendText("Second paragraph.")
      Call doc.Save(True, True)
    End Sub
  4. This agent is the same as the preceding one but uses New instead of CreateRichTextItem.
    Dim session As NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Dim rti As NotesRichTextItem
    
    Sub Initialize
      Set session = New NotesSession
      Set db = session.CurrentDatabase
      Set doc = db.CreateDocument
      Call doc.ReplaceItemValue("Form", "Main Topic")
      Call doc.ReplaceItemValue("Subject", "NotesRichTextItem New")
      Set rti = New NotesRichTextItem(doc, "Body")
      Call rti.AppendText("First paragraph.")
      Call rti.AddNewLine(2)
      Call rti.AppendText("Second paragraph.")
      Call doc.Save(True, True)
    End Sub
  5. This example creates several rich text items and sets styles for them.
    Sub Initialize
      Dim session As New NotesSession    
      Dim db As NotesDatabase  
      Set db = session.CurrentDatabase  
      Dim doc As New NotesDocument(db)  
      Call doc.AppendItemValue("From", session.UserName)  
      Call doc.AppendItemValue("Subject", _   
        "Meeting time changed")    
      Dim richStyle As NotesRichTextStyle   
      Set richStyle = session.CreateRichTextStyle    
      Dim richText As New NotesRichTextItem(doc, "Body")  
      Call richText.AppendText("The meeting is at 2pm.")  
      richStyle.Bold = True  
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2pm.")
      richStyle.Italic = True   
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2pm.")
      richStyle.FontSize = 18   
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2pm.")
      richStyle.NotesColor = COLOR_CYAN  
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2pm.")
      richStyle.Effects = EFFECTS_SHADOW
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2pm.")
      richStyle.Strikethrough = False    
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2pm.")
      richStyle.Underline = True    
      Call doc.Save(True, False)
    End Sub
  6. This example creates a rich text item and marks it as pass-thru HTML.
    Sub Initialize    
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Set db = session.CurrentDatabase
      Dim doc As New NotesDocument(db)
      Call doc.AppendItemValue("From", session.UserName)
      Call doc.AppendItemValue("Subject", _
        "Meeting time changed")  
      Dim richStyle As NotesRichTextStyle
      Set richStyle = session.CreateRichTextStyle
      Dim richText As New NotesRichTextItem(doc, "Body")
      richStyle.PassThruHTML = True
      Call richText.AppendStyle(richStyle)
      Call richText.AppendText("The meeting is at 2:00.")
      Call doc.Save(True, False)
    End Sub
  7. This example creates a rich text item and displays its rich text paragraph properties.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase  
      Set db = session.Currentdatabase
      Dim doc As New NotesDocument(db)
      Call doc.AppendItemValue("From", session.Username)
      Call doc.AppendItemValue("Subject", _
      "Meeting agenda")
      Dim rtpStyle As NotesrichTextParagraphStyle
      Set rtpStyle = session.CreateRichTextParagraphStyle
      Dim richText As New NotesRichTextItem(doc, "Body")
      Call richText.AppendText("Where are we now?")
      Messagebox _ 
      "Alignment: " & rtpStyle.Alignment & Chr(10) _
      & "FirstLineLeftMargin: " & _
         rtpStyle.FirstLineLeftMargin & Chr(10) _ 
      & "Inter-line Spacing: " & _
         rtpStyle.InterLineSpacing & Chr(10) _
      & "Left Margin: " & rtpStyle.LeftMargin & Chr(10) _
      & "Right Margin: " & rtpStyle.RightMargin & Chr(10) _
      & "Spacing Above: " & _
         rtpStyle.SpacingAbove & Chr(10) _
      & "Spacing Below: " & _
         rtpStyle.SpacingBelow & Chr(10) _
      & "Tabs: " & rtpStyle.Tabs)
      Call doc.Save(True, False)
    End Sub
  8. This example sets the rich text alignment property of a rich text paragraph style object.
    Sub Initialize
      Dim session As New NotesSession  
      Dim db As NotesDatabase
      Set db = session.Currentdatabase
      Dim doc As New NotesDocument(db)
      Call doc.AppendItemValue("From", session.Username)
      Call doc.AppendItemValue("subject", _
      "Meeting agenda")
      Dim rtpStyle As NotesRichTextParagraphStyle
      Set rtpStyle = session.CreateRichTextParagraphStyle
      Dim richText As New NotesRichTextItem(doc, "Body")  
      rtpStyle.Alignment = ALIGN_CENTER
      Call richText.AppendParagraphStyle(rtpStyle)
      Call richText.AppendText("Q1 Report")
      Call doc.Save(True, False)
    End Sub
  9. This example sets the amount of space surrounding a rich text paragraph style object.
    Sub Initialize  
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Set db = session.Currentdatabase
      Dim doc As New NotesDocument(db)
      Call doc.AppendItemValue("From", session.Username)
      Call doc.AppendItemValue("subject", _
      "Meeting agenda")
      Dim rtpStyle As NotesrichTextParagraphStyle
      Set rtpStyle = session.CreateRichTextParagraphStyle
      Dim richText As New NotesRichTextItem(doc, "Body") 
      rtpStyle.SpacingAbove = SPACING_DOUBLE 
      rtpStyle.SpacingBelow = SPACING_ONE_POINT_50
      Call richText.AppendParagraphStyle(rtpStyle)
      Call richText.AppendText("Q1 Report: State of the company")
      Call doc.Save(True, False)
    End Sub
  10. This example sets a tab in a rich text paragraph style object.
    Sub Initialize  
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Set db = session.CurrentDatabase
      Dim doc As New NotesDocument(db)
      Call doc.AppendItemValue("From", session.Username)
      Call doc.AppendItemValue("subject", _
      "Meeting agenda")
      Dim rtpStyle As NotesrichTextParagraphStyle
      Set rtpStyle = session.CreateRichTextParagraphStyle
      Dim richText As New NotesRichTextItem(doc, "Body")
      Call richText.AppendParagraphStyle(rtpStyle)
      Call richText.AppendText("Meeting agenda")
      Messagebox "Number of Tabs: " & rtpStyle.Tabs
      Call doc.Save(True, False)
    End Sub
  11. This example sets tabs at a specified interval in a rich text paragraph style object.
    Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Set db = session.CurrentDatabase
      Dim doc As New NotesDocument(db)
      Call doc.AppendItemValue("From", session.UserName)
      Call doc.AppendItemValue("Subject", _
        "Meeting time changed")
      Dim rtpStyle As NotesRichTextParagraphStyle
      Dim pos As Long
      Dim interval As Long
      Set rtpStyle = session.CreateRichTextParagraphStyle
      pos = RULER_ONE_INCH
      interval = RULER_ONE_CENTIMETER
      Call rtpStyle.SetTabs(3, pos, interval, TAB_DECIMAL)
      Dim richText As New NotesRichTextItem(doc, "Body")
      Call richText.AppendParagraphStyle(rtpStyle)
      Call richText.AddTab(1)
      Call richText.AppendText("The meeting is a 3:00.")
      Call doc.Save(True, False)
    End Sub