Modify a rich text field in the UI without having to save and re-open

With this LotusScript code, you can modify any rich text fields and reflect those changes in the UI without being forced to save the document.

View member feedback to this tip.

One big issue with rich text fields is that "Modifications made to rich text items on the back-end document do not appear on the current document until it is closed and reopened." This means that any code you might have in the UI accessing a rich text field (create tables, append doc links, etc.) won't immediately show up without saving and re-opening.

Until now, either you saved and re-opened the back-end doc to reflect the changes to the rich text field (which meant that you also had to incorporate some sort of clean-up code if a user chose to cancel the save) or you don't reflect the changes in the UI, which can cause some user confusion ("yes, you did make the changes but you have to save and re-open the doc to see them").

With the following code, you can modify any rich text fields and reflect those changes in the UI without being forced to save the document. In this example, I am appending a document link but I have tested the code for creating a table as well. The user may notice a slight flicker as the document is closed and re-opened.

One other application for this code is Computed Subforms. In the Designer Help, it is stated that "Subform formulas cannot be refreshed while the document is open." This method of re-initializing the document in memory allows the Computed Subform to change.

Sub Click(Source As Button)
Dim s As New NotesSession
Dim ws As New NotesUIWorkspace
Dim thisdb As NotesDatabase
Dim coll As NotesDocumentCollection 
' collection returned from
PickListCollection
Dim otherdoc As NotesDocument 
' this is the doc to create a link to
Dim thisdoc As NotesDocument 
' the new doc that the link is being added to
Dim rtitem As NotesRichTextItem 
' required for AppendDocLink
Dim nitem As Variant 
' used to get a handle on the 
NotesRichTextField
Dim olduidoc As NotesUIDocument 
' the original instance of the uidoc
Dim newuidoc As NotesUIDocument 
' the new instance of the uidoc after the doc
link has been added

Set thisdb = s.CurrentDatabase
Set olduidoc = ws.CurrentDocument 
' current uidoc
Set thisdoc = olduidoc.Document 
' doc in memory but hasn't been saved yet

' select the doc to link to
Set coll = ws.PickListCollection
( PICKLIST_CUSTOM, False, thisdb.Server,
thisdb.FilePath, "luProject", 
"Project List", "Please select a project." )

' if a doc isn't selected exit
If coll.Count = 0 Then
Messagebox "User canceled" ,,
"No project was selected."
Exit Sub
End If

' get the doc to link to
Set otherdoc = coll.GetFirstDocument

' grab some values from that doc
thisdoc.ProjectName = 
otherdoc.ProjectName
thisdoc.CustomerName = 
otherdoc.CustomerName

' get the RichTextField in the 
current uidoc 
Set nitem = 
thisdoc.GetFirstItem( "ProjectDocLink" )

' add the doc link
' NOTE: this is being done to
 the backend doc that exists in memory
If ( nitem.Type = RICHTEXT ) Then
Set rtitem = nitem
Call rtitem.AppendDocLink(otherdoc,
 "Original Doc Link", "")
Call rtitem.Update
End If

' set the SaveOptions field so that 
when the uidoc is closed, 
the user won't be asked to save
thisdoc.SaveOptions = "0"

' close the uidoc. It won't actually 
happen until the code is finished executing
Call olduidoc.Close(True)

' create a new uidoc and open the
 backend doc that is still in memory with 
added doc link
Set newuidoc = ws.EditDocument(True, thisdoc)

' delete the reference to the old uidoc
' this is necessary because the 
code below affects it if left in memory
Delete olduidoc

' re-associate the variable with the backend doc
' have to do this because the 
olduidoc reference was deleted
Set thisdoc = newuidoc.Document

' remove the SaveOptions field
 so the doc can be saved
Call thisdoc.RemoveItem( "SaveOptions" )

End Sub 

MEMBER FEEDBACK TO THIS TIP

This works well, but the document you are working with must have been saved first. If it has not been saved, when the line of code shown below runs, you will get an error because the field is not actually a rich text field element yet. It will only be that after the document has been saved at least once.

If ( nitem.Type = RICHTEXT ) then you will receive this error if the document has not been saved:

Figure 1

—Steve H.

******************************************

I have tried to use to code to embed an attachment in the rich text field body, but I can not get rid of the field save options.

Sub Click(Source As Button) 
Dim s As New NotesSession 
Dim ws As New NotesUIWorkspace 
Dim thisdb As NotesDatabase 
Dim coll As NotesDocumentCollection 
' collection returned from 
        
Dim otherdoc As NotesDocument 
' this is the doc to create a link to 
Dim thisdoc As NotesDocument 
' the new doc that the link is being 
added to 
Dim rtitem As NotesRichTextItem 
' required for AppendDocLink 
Dim nitem As Variant 
' used to get a handle on the 
NotesRichTextField 
Dim olduidoc As NotesUIDocument 
' the original instance of the uidoc 
Dim newuidoc As NotesUIDocument 
' the new instance of the uidoc after 
the doc link has been added 
        
Const SCANFILEPATH$=
{C: \ Tempscan.jpg} 
Set thisdb = s.CurrentDatabase 
Set olduidoc = ws.CurrentDocument 
' current uidoc 
Set thisdoc = olduidoc.Document 
' doc in memory but hasn't been saved 
yet 
        
        
Set nitem = thisdoc.GetFirstItem( "Body" ) 
If ( nitem.Type = RICHTEXT ) Then 
Set rtitem = nitem 
Set object = rtitem.EmbedObject 
( EMBED_ATTACHMENT, "",        
SCANFILEPATH ) 
Call rtitem.Update 
End If 
        
thisdoc.SaveOptions = "0" 
Call olduidoc.Close(True) 
Set newuidoc = 
ws.EditDocument(True, thisdoc) 
        
Delete olduidoc 
Set thisdoc = newuidoc.Document 
Call thisdoc.RemoveItem( "SaveOptions" )         
        
End Sub

—Anonymous

******************************************

You can try modifying the code as follows:

Set thisdoc = olduidoc.Document 
' doc in memory but hasn't been saved 
yet

' if the doc is new, adds a 
new RichTextField in the current doc If 
thisdoc.IsNewNote Then
Set rtitem = 
thisdoc.CreateRichTextItem( "Body" ) End If

Set nitem = thisdoc.GetFirstItem( "Body" )

Also, if the form you are working with is not the default form in the database, you should also add a Computed Form field to the form so that when the code closes and re-opens the document, it knows what form to use.
— Bryce Berry, tip author

******************************************

I thought this tip would be the thing, but I then found an easier way. I hope this is useful. Also, I'm using VBA / Excel.

On Error Resume Next 
        MailDoc.Send False, "" 
On Error GoTo 0 
MailDoc.SendTo = SendToAdr 
Set workspace = 
CreateObject("Notes.NotesUIWorkspace") 
Call workspace.EDITDOCUMENT
(True, MailDoc, True).GOTOFIELD("Body") 

I send the doc to an empty/blank address, and trap and ignore the error. When I create the uidoc, my body appears, and I didn't have to save the doc first.
—Michael R.

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Bryce Berry. Please let others know how useful it is via the rating scale above. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close