例: Working with text in LotusScript® classes

  1. 次のエージェントは、一度に 1 つの段落のリッチテキストアイテム内のテキストを取得します。
    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. 次のエージェントは、リッチテキストアイテム内の段落を取得し、さらに各段落内のテキストランを取得します。
    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. 次のエージェントは、リッチテキストアイテムを作成し、2 段落分のテキストを挿入します。
    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. 次のエージェントは上記と同じですが、CreateRichTextItem の代わりに New を使用します。
    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. 次の例では、いくつかのリッチテキストアイテムを作成してスタイルを設定します。
    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. 次の例では、リッチテキストアイテムを作成してパススルー 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. 次の例ではリッチテキストアイテムを作成し、そのリッチテキスト段落プロパティを表示します。
    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. 次の例では、リッチテキスト段落スタイルオブジェクトのリッチテキスト文字揃えプロパティを設定します。
    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. 次の例は、リッチテキスト段落スタイルオブジェクトの周囲の間隔を設定します。
    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. 次の例ではリッチテキスト段落スタイルオブジェクトにタブを設定します。
    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. 次の例では、リッチテキスト段落スタイルオブジェクトに指定された間隔でタブを設定します。
    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