1
votes

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?

3

3 Answers

2
votes

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.
0
votes

The first problem is that when you click the action button, you will lose focus of the rich text field, and therefor there is no way to know where the cursor was.

I would also suggest that you don't use the extended notation like this: string1 = docFill.sampleText1(0) Use the GetItemValue method of the NotesDocument class instead (for several reasons, including performance and future-proofing your code.

If you just want to have the user enter some text, why not use the InputBox function?

Finally, there is not an easy way to insert text in the middle of a rich text. It is much easier to perform a replace of a specific text string in a rich text field. I once created a Lotusscript class to perform mailmerge (create letters based on a template and a form letter with field names and commands), you can find it here: http://blog.texasswede.com/code-mail-mergeform-letters-in-lotuscript/

Perhaps that can help you some. But it has to be done in the backend, you can't do much rich text work in the frontend, unless you use Midas LSX frpn Ben Langhinrichs (http://www.geniisoft.com). I think he got some UI functionality.

But your biggest issue will be the first problem, how to trigger the code without losing focus of the rich text field. I don't see a good solution there. You may want to rethink your design/approach.

0
votes

If you use a button in the Action Bar then the focus will remain with the rich text field. You can then use uidoc.InsertText("") to insert the text at the current position of the cursor.

You could use... call uidoc.InsertText( docFill.sampleText1(0) )