Workaround for moving rich text data to a computed richtext field

After many many hours of search this forum and trying diffrent coding approaches I finally found a solution for moving data from an editable richtext field to a computed one. As with most ways of working with richtext the document must be saved and then reopened. Also many people have posted a method of doing this that uses the copy and paste commands, that method works fine for release 5 but there is a bug in release 6 that will intermititaly cause the loss of attachments. I have extensivly tested this code in both R5 and R6 with almost every type of object you can put into a rich text field and it’s working flawlesly. If you find a situation that this code does not work with please post a reply here.

To test this code create a form with 2 richtext fields “RichText1” (computed formula = RichText1) and “RichText2” (editable). Add a button the the form with the code below.

I hope this save someone out there the weeks of time i have spent on this problem.

James :slight_smile:

Sub Click(Source As Button)

Dim ws As New NotesUIWorkspace

Dim unid As String									'ID to users document

Dim unid2 As String								'ID to temp document

Call ws.CurrentDocument.Save()				'Make sure back end document has RT data

unid = ws.CurrentDocument.Document.UniversalID

Call ws.CurrentDocument.Close()



Dim Session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim RT1 As NotesRichTextItem

Dim RT2 As NotesRichTextItem

Set db = session.CurrentDatabase

Set doc = db.GetDocumentByUNID(unid)		'Get document from db with unid do not use ui methods here

Set RT2 = doc.GetFirstItem("RichText2")		'This is the data we want to move



Dim doc2 As New NotesDocument(db)										'Create temporary document

doc2.form = "RichTextHold"

Dim RTHold As New NotesRichTextItem( doc2 , "RichTextHold")	'Add a RT item to hold data to move

Call RTHold.AddNewline(2)

Call RTHold.AppendText( "***********************************************************************************" )

Call RTHold.AddNewline(1)	

Call RTHold.AppendText( "This data added by " + session.CommonUserName + " on " + Cstr(Now) )

Call RTHold.AddNewline(1)

Call RTHold.AppendRTItem( RT2 )												'Move data from users document

Call RTHold.AddNewline(2)

Call RTHold.AppendText( "***********************************************************************************" )

Call RTHold.AddNewline(1)

Call doc2.Save( True , False )

unid2 = doc2.UniversalID



'This is a must.  If you do not delete the current memory reference to the documents then notes

'	will reuse handels to so of the items (attachments) inside the RT field.

If Not doc Is Nothing Then Delete doc

If Not doc2 Is Nothing Then Delete doc2



'Reload the documents.  Now notes does not have any idea the the two RT items contain

'	the same data.

Set doc = db.GetDocumentByunid(unid)

Set doc2 = db.GetDocumentByunid(unid2)

Set RTHold = doc2.GetFirstItem("RichTextHold")

'Must use CopyItemToDocument here.  All other methods that were tried failed.

Set RT1 = RTHold.CopyItemToDocument( doc, "RichText1" )

Call doc.Save( True , False )

'We don't need the temporary document anymore and can delete it.

'If the user does not have delete rights the these documents will have to be cleaned up be an agent.

Call doc2.Remove( True )



If Not doc Is Nothing Then Delete doc



Set doc = db.GetDocumentByunid(unid)

Call ws.EditDocument( True , doc )			'Reload document with the RT contents now in the computed RT field.

End Sub