Use Lotus script to flexibly operate rich text fields in Lotus Notes

Source: Internet
Author: User
Chen Bin, Senior Software Engineer, IBM Chen Yun, software engineer, IBM Introduction: This article describes how to use LotusScript for flexible operations. Lotus Notes Rich Text domain (Rich Text Field), and provides several examples Program . Users are required to have LotusScript. Programming Experience and proficiency in using Lotus Domino designer.


Introduction



For a long time, rich text fields of Lotus Notes are frequently used. Rich Text fields are used in almost any domino application. Rich Text fields of Lotus Notes are also very powerful. In addition to common text, they also support images, tables, embedded objects, HTTP links, notes links, attachments, and many other types. However, there is a problem that has long plagued LotusScript. it is difficult for developers to flexibly operate various types of content in the Notes Rich Text domain. In fact, many new Lotus scripts have been added during the constant upgrade of Domino. class to operate the rich text fields of notes, but many Lotus scripts. developers are not familiar with this. This article describes how to use these classes to flexibly operate rich text fields.



Lotus script. Class Related to rich text fields in notes



Lotus script. classes related to the operations on the rich text fields of notes include:


    • Notesrichtextnavigator: the navigator of rich text fields, used to access various elements in rich text fields;
    • Notesrichtextrange indicates a range of rich text fields, which can contain multiple elements;
    • Notesrichtextdoclink indicates the document link of Rich Text domain content;
    • Notesembeddedobject Embedded Object or file attachment;
    • Notesrichtextsection indicates a section in the rich text field;
    • Notesrichtexttable indicates a table in the rich text field;
    • Notesrichtextstyle. It indicates various attributes of rich text;
    • Notesrichtextparagraphstyle. It indicates the attributes of rich text paragraphs;
    • Notescolorobject indicates a color.


Example



Next we will use a program to analyze the usage of each class.



First, create a blank Domino application and create a form named "test", which only contains a rich text field named "body, create an action named "test" in the default view ",CodeAs follows. This program generates a document that contains a rich text field and various elements in the rich text field.


Listing 1. Generating various elements


Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Dim doc As NotesDocument
    Set db = s.CurrentDatabase
    Set doc = New NotesDocument(db)
    doc.Form. = "test"
    Dim rtf As NotesRichTextItem
    Set rtf = doc.CreateRichTextItem ("Body")
    
    'Generate a text paragraph and set its font size, color and other attributes
    Dim style. As NotesRichTextStyle. Set style. = s.CreateRichTextStyle. Dim pstyle. As NotesRichTextParagraphStyle. Set pstyle. = s.CreateRichTextParagraphStyle. Dim color As NotesColorObject
    Set color = s.CreateColorObject
    style.FontSize = 20
    style.Bold = True
    pstyle.Alignment = ALIGN_LEFT
    pstyle.FirstLineLeftMargin = RULER_ONE_INCH
    Call color.SetRGB(123, 234, 123)
    style.NotesColor = color.NotesColor
    Call rtf.AppendStyle(style)
    Call rtf.AppendParagraphStyle(pstyle)
    Call rtf.AppendText("This is a text paragraph, aligned to the left.")
    Call rtf.AddNewline(1)
    
    'Generate a database link, link to the current database
    Call rtf.AppendDocLink(db, "Link to the current database", "Current database")
    
    'Generate a section containing a table
    Call rtf.AppendStyle(style)
    Call rtf.BeginSection("This is a section", style, color, True)
    Call rtf.AppendText("This is the beginning of the section")
    iRow% = 3
    iCol% = 3
    style.NotesColor = COLOR_BLUE
    Call rtf.AppendStyle(style)
    'Add a 3X3 form
    Call rtf.AppendTable(iRow%, iCol%)
    Call rtf.AppendText("This is the end of the section")
    Call rtf.EndSection
    Dim nav As NotesRichTextNavigator
    Set nav = rtf.CreateNavigator
    Call nav.FindFirstElement(RTELEM_TYPE_TABLECELL)
    style.FontSize=16
    style.Bold=False
    Call rtf.AppendStyle(style)
    For i% = 1 To iRow%
        For j% = 1 To iCol%
            Call rtf.BeginInsert(nav)
            Call rtf.AppendText("line" & i% & ", column "& j%)
            Call rtf.EndInsert
            Call nav.FindNextElement(RTELEM_TYPE_TABLECELL)
        Next
    Next
    
    'Add an attachment
    Call rtf.EmbedObject(EMBED_ATTACHMENT, "", "C:\Documents and Settings\All Users\
        Documents\My Pictures\Sample Pictures\Water lilies.jpg")
    
    Call doc.Save(True,True)
End Sub







The following describes some of the methods used in this program.


  • Notessession. createrichtextstyle: Creates a notesrichtextstyle. object.
  • Notessession. createrichtextparagraphstyle: Creates a notesrichtextparagraphstyle. object.
  • Notessession. createcolorobject: Creates a notescolorobject object.

    Note that notesrichtextstyle, notesrichtextparagraphstyle. And notescolorobject objects cannot be created using new, because these three classes do not have the new () method and can only be created using notessession.

  • Notesrichtextitem. appendstyle: Insert a format object in the current position. The format after this position is used until another format is inserted.
  • Notesrichtextitem. appendparagraphstyle: insert a paragraph format object in the current position. This format is used for paragraphs after this position until another paragraph format is inserted.
  • Notesrichtextitem. beginsection: Insert a section in the rich text domain.
  • Notesrichtextitem. endsection: the end of the section. It must be paired with beginsection.

    You can use various append methods to add various elements between the two methods. When inserting segments, the segments are always at the end of the rich text field. Note that you cannot create a section that contains the existing elements in the rich text domain. The Section created by using the beginsection method is always empty, and the section content must be added by the program.

  • Notesrichtextitem. createnavigator: Creates a Rich Text domain navigator object notesrichtextnavigator.

    Only this method can be used to create a Rich Text domain navigator. There is no new method for notesrichtextnavigator. You can also obtain a notesrichtextnavigator object by using the notesrichtextrange. Navigator attribute.

  • The notesrichtextnavigator class is the most important class for flexible access to the content in rich text fields. through some of its methods, you can easily access elements in rich text fields.

    The navigator object notesrichtextnavigator maintains a current location. Any get or find operation in the rich text domain may change this location. Navigation always occurs in the same type of elements. You can use the find and get methods to access the corresponding elements. After finding the required elements, you can use the get method to obtain the elements. The following table lists these methods.


Table 1. Method list


Method Name Description
Findfirstelement Move the current position to the first element of the specified type
Findnextelement Move the current position to the next element of the specified type
Findlastelement Move the current position to the last element of the specified type
Findnthelement Move the current position to the nth element of the specified type
Findfirststring Move the current position to the beginning of the first specified string
Findnextstring Move the current position to the beginning of the next specified string
Getelement Returns the element at the current position.
Getfirstelement Returns the first element of the specified type.
Getlastelement Returns the last element of the specified type.
Getnextelement Returns the next element of the specified type.
Getnthelement Returns the nth element of the specified type.





Notesrichtextitem. begininsert: Change the insert position from the end of the rich text field to the start or end of the specified element.



Notesrichtextitem. endinsert: resets the insert position to the end of the Rich Text Field and needs to be paired with begininsert.



You can use various append methods to add various elements between the two methods. Let's take a look at the specific usage of begininsert:



Call notesrichtextitem. begininsert (element, [after])



Parameter description



Element: It can be vertex, notesrichtextdoclink, notesrichtextnavigator, notesrichtextrange, notesrichtextsection, or notesrichtexttable, indicating the location of the object. If it is vertex, it indicates the current location of the notesrichtextnavig.



After: an optional Boolean parameter. "True" indicates that the insert position is at the end of the element. "false" indicates that the insert position starts with the element.



Through the simple example above, we can see how to use LotusScript. to operate rich text fields. Next we will use another example to demonstrate how to use LotusScript. to implement a simple function similar to converting text and tables in Word. It mainly shows how to use the notesrichtextrange class to operate text paragraphs in rich text fields.



First, create a form test, create a rich text field of the body in the form, and then create a form operation named text2table to convert the text into a table. The Code is as follows:


Listing 2 converts text to a table


Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim ws As New NotesUIWorkspace
    Dim uidoc As NotesUIDocument
    Set uidoc = ws.CurrentDocument
    Dim doc As NotesDocument
    Set doc = uidoc.Document
    Dim rtf As NotesRichTextItem
    Set rtf = doc.GetFirstItem("Body")
    'Set the separator to a space
    delimiter$ = ""
    rowcount% = 0
    colcount% = 0
    Dim rtnav As NotesRichTextNavigator
    Set rtnav = rtf.CreateNavigator
    Dim rtrange As NotesRichTextRange
    Dim rows()
    Dim paraArray As Variant
    Dim paraStr As String
    Dim firstTime As Boolean
    firstTime = True
    
    If rtnav.FindFirstElement(RTELEM_TYPE_TABLECELL) Then
        Msgbox "Form already exists!"
        Exit Sub
    End If
    If rtnav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH) Then
        Set rtrange = rtf.CreateRange
        Do
            'Set the beginning of the text range to the position pointed to by rtnav
            Call rtrange.SetBegin(rtnav)
            'Get the text paragraph at that position
            paraStr = rtrange.TextParagraph
            paraArray = Split(paraStr)
            'Use firstTime to determine whether the format of the text paragraph can be converted into a table
            If firstTime Then
                colcount% = Ubound(paraArray)
                firstTime = False
            Else
                If colcount% <> Ubound(paraArray) Then
                    Msgbox "Text cannot be converted into a table!"
                    Exit Sub
                End If
            End If
            'Define a dynamic array to save all text paragraphs
            Redim Preserve rows(rowcount%)
            rows(rowcount%) = paraArray
            rowcount% = rowcount% + 1
        Loop While rtnav.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)
    Else
        Messagebox "There is no text in the rich text field"
        Exit Sub
    End If
    'Clear the rich text field value
    rtf.Values = ""
    rowcount% = rowcount%-1
    'Insert the table, and insert the saved text into the corresponding table cell in turn
    Dim row As Variant
    Call rtf.AppendTable(rowcount%+1, colcount%+1)
    Call rtnav.FindFirstElement(RTELEM_TYPE_TABLECELL)
    For i% = 0 To rowcount%
        row = rows(i%)
        For j% = 0 To colcount%
            Call rtf.BeginInsert(rtnav)
            Call rtf.AppendText(row(j%))
            Call rtf.EndInsert
            Call rtnav.FindNextElement(RTELEM_TYPE_TABLECELL)
        Next
    Next
    'Save the document and reopen it to refresh
    Call doc.Save(True, True)
    Call uidoc.Close(True)
    Call ws.EditDocument(False,doc)
    
End Sub





Create a form operation named table2text to convert the table to text. The Code is as follows:


Listing 3 converts a table to text


Sub Click(Source As Button)
    Dim s As New NotesSession
    Dim ws As New NotesUIWorkspace
    Dim uidoc As NotesUIDocument
    Set uidoc = ws.CurrentDocument
    Dim doc As NotesDocument
    Set doc = uidoc.Document
    Dim rtf As NotesRichTextItem
    Set rtf = doc.GetFirstItem("Body")
    
    delimiter$ = ""
    Dim rtnav As NotesRichTextNavigator
    Set rtnav = rtf.CreateNavigator
    Dim rtrange As NotesRichTextRange
    Set rtrange = rtf.CreateRange
    Dim tbl As NotesRichTextTable
    Dim rowcount As Integer
    Dim colcount As Integer
    
    If rtnav.FindFirstElement(RTELEM_TYPE_TABLE) Then
        Set tbl = rtnav.GetElement
        rowcount = tbl.RowCount-1
        colcount = tbl.ColumnCount-1
        Redim data(rowcount, colcount)
        For i% = 0 To rowcount
            For j% = 0 To colcount
                Call rtnav.FindNextElement(RTELEM_TYPE_TABLECELL)
                Call rtrange.SetBegin(rtnav)
                If j% = colcount Then
                    Call rtf.AppendText(rtrange.TextParagraph)
                Else
                    Call rtf.AppendText(rtrange.TextParagraph & delimiter$)
                End If
            Next
            If i% <rowcount Then
                'Generate a new paragraph
                Call rtf.AddNewline(1)
            End If
        Next
    Else
        Messagebox "There are no tables in the rich text field"
        Exit Sub
    End If
    
    Call tbl.Remove
    Call doc.Save(True, True)
    Call uidoc.Close(True)
    Call ws.EditDocument(False,doc)
End Sub






Summary



Based on the introduction and examples and code analysis in this article, I believe that you have an understanding of how to operate the rich text fields in notes. We can see the LotusScript. by using appropriate classes flexibly, we can accomplish many functions that we think cannot be completed. I hope this article will be helpful to readers.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.