API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Creating Sections Programmatically
Building sections in a Notes 6 rich text field is actually pretty easy. You call "BeginSection" at the point you want the section to start, then "EndSection" when you are finished with the section. The "BeginSection" takes anywhere from 1 to 4 parameters. The first is the title of the section, the second is a NotesRichTextStyle object that will be used to define the style of the section, the third is a NotesColorObject object that will be used to define the color of the twisty, and the fourth is a Boolean value of True or False to indicate if the section should be expanded or collapsed. The default is that the section should be collapsed, so you can create a collapsed section with the default green twisty by just supplying one parameter.

The code below will go to your mail file, create a new document, create a rich text item in the new document, then put in two sections - one expanded and one collapsed. The code is pretty straightforward, so I'll just let you take a look at it:

Sub Initialize
   Dim session As New NotesSession
   Dim mailDb As New NotesDatabase("", "")
   Dim ws As New NotesUIWorkspace
   Dim doc As NotesDocument
   Dim body As NotesRichTextItem
   Dim style As NotesRichTextStyle
   Dim color As NotesColorObject
   
   Call mailDb.OpenMail
   Set doc = mailDb.CreateDocument
   Call doc.ReplaceItemValue("Form", "Memo")
   Set body = doc.CreateRichTextItem("Body")
   Set style = session.CreateRichTextStyle
   Set color = session.CreateColorObject
   Call body.AppendText("This is some text before the section")
   Call body.AddNewline(2)
   Call body.BeginSection("Expanded Section", style, color, True)
   Call body.AppendText("Here is some text within the section")
   Call body.AddNewline(2)
   Call body.AppendText("Here is some more text within the section")
   Call body.EndSection
   Call body.AddNewline(2)
   Call body.AppendText("This is some text between the two sections")
   Call body.AddNewline(2)
   Call body.BeginSection("Collapsed Section")
   Call body.AppendText("Here is some text within the section")
   Call body.AddNewline(2)
   Call body.AppendText("Here is some more text within the section")
   Call body.EndSection
   Call body.AddNewline(2)
   Call body.AppendText("This is some text after the section")
   Call doc.Save(True, False, False)
   Call ws.EditDocument(True, doc)
   Call doc.Remove(True)
End Sub

Note that you're not allowed to create a table inside the open section using the NotesRichTextTable class. Your LotusScript will error out. So you're better off sticking to the "old" techniques of just adding paragraph styles, text styles, and appending text.

The code above creates a NotesRichTextStyle object and a NotesColorObject object, but never does anything with them except pass them into the "BeginSection" method for the expanded section. There's lots of settings you can do with those objects before passing them in as parameters to the BeginSection method, but here we just wanted to show section creation and not dive into those other classes (yet).