Search code examples
lotus-noteslotusscript

How to insert text and possible rich text as well at the current position of the cursor in a rich text field in Lotus Script?


At first, there's a rich text field in the form that has a text inputted already (for this scenario - "hello world"). I have placed the cursor just after the letter "o" from "hello". I have a button that will open up a dialog box with one text field and I was wondering how would you be able to insert the text from that field from the dialog box at the current position of the cursor in the rich text field.

So far the code I have is:

Sub Click(Source As Button)
    Dim workspace As New NotesUIWorkspace
    Dim s As New NotesSession
    Dim db As NotesDatabase
    Set db = s.currentdatabase
    Dim docFill As New notesdocument(db)
    Call workspace.DialogBox _
    ( "Test", True, True, False, False, _
    False, False, "Test Insert text at current position in rich text field", docFill, True, False, True )

    Dim string1 As String

    string1 = docFill.sampleText1(0)

    Dim rts As NotesRichTextStyle
    Set rts = s.CreateRichTextStyle

    End Subs
End Sub

Let's say I entered "stackoverflow" in sampleText1 text field. After clicking ok then it will be inserted at the position of the cursor in the rich text field. So the result will be "hellostackoverflow world".

Also just an additional question. Let's say I also wanted the text to be in color red or a different font so I would be using notesrichtextstyle class etc to design it. How would you be able to insert the rich text at the position of the cursor in the rich text fiel if that would be the case?


Solution

  • You can insert text at current cursor position with the help of the clipboard. Just let the user insert text in a dialog box, select the text after clicking "OK", copy it and then paste it at the current cursor position in RichText field back in your form.

    To accomplish that, create an action "Insert text" in your form's action bar with the LotusScript code

    Sub Click(Source As Button)
        Dim workspace As New NotesUIWorkspace
        Dim uidoc As NotesUIDocument
        Dim doc As NotesDocument    
        Set uidoc = workspace.CurrentDocument
        Set doc = uidoc.Document
        If workspace.DialogBox _
            ("Dlg", True, True, False, True, _
            True, False, "Test insert text", doc, True, False, True ) Then
                    uidoc.Paste
        End If  
    End Sub
    

    Actions in action bars have the advantage that they don't change the cursor position in document on click event. So, the cursor still remains on current position e.g in RichText field clicking on an action button.

    Then, create a form "Dlg" for DialogBox with a Text or RichText field "Text". Add the following formula code to form's Postrecalc event (it's executed on "OK" button click):

    @Command([EditGotoField]; "Text");
    @Command([EditSelectAll]);
    @Command([EditCopy]) 
    

    You have a lot of options with the copy-paste-approach to put content into clipboard:

    • Create a text in backend and put it direct into clipboard
    • Create a RichText item in a temp document with all content and style options you can think of, open the document in UI, copy the RichText item content to clipboard and close document without saving
    • Let users create their text snippet in documents. Let them choose one of it clicking on "Insert text" button - just open the selected document, copy the content to clipboard and close it.