Lotusscript and Domino musings

 
alt

Lars Berntrop-Bos

 

RTF appending on the same document using LotusScript

Lars Berntrop-Bos  February 21 2024 02:42:10 PM
How to copy some Rich Text to from one field to another and not have to fool around with attachments
Simple, if you use this recipy.


Use case:

You have a form with a RichText field (Editable), the contents of which you want to add to another RichText field (computed), keeping the attachments. Excellent for keeping history.


Solution:

%REM
     Sub PostSave
     Prepend Comment to History Field
%END REM

Sub
PostSave(Source As NotesUIDocument)
     
Dim doc As NotesDocument
     
Dim rtReport As NotesRichTextItem
     
Dim rtReport As NotesRichTextItem
     
Dim rtTemp As NotesRichTextItem
     
Dim rtComment As NotesRichTextItem
     
Dim mustAddPart As Boolean
     
     
Set ws = New NotesUIWorkspace
     
Set doc = Source.Document
     
     
' get comment field, it's nam is convAddPart
     
Set rtComment = doc.GetFirstItem("convAddPart")
     
' Is there text in the Comment, or an attachment?
     mustAddPart =
Len(rtComment.Abstract(5, False, False)) > 0 Or Not IsEmpty(rtComment.EmbeddedObjects)
   
     
If mustAddPart Then
             
' Insert commentaar in start of history field, name is convReport
             
Set rtHistory = doc.GetFirstItem("convReport")
             wipeRT doc,
"convAddReportTemp"' make sure temp is empty
             
Set rtTemp = New NotesRichTextItem(doc, "convAddReportTemp") ' create temp RT
             rtTemp.AppendRTItem rtHistory
' save old report
             
Delete rtHistory ' kill object
             wipeRT doc,
"convReport" ' remove in doc, but leave $file
           
             
' now start building the report
             
Set rtHistory = New NotesRichTextItem(doc, "convReport") ' new item
             
' call helper routine that adds a bold header to rtHistory, and a line end
             
Call appendHeaderPlusTextToRT(rtHistory, Format$(Now, "dd/mm/yyyy hh:mm:ss") + ", " + ses.CommonUserName, "") ' user and timestamp in bold
             
' get Comment
             
Set rtComment = doc.GetFirstItem("convAddpart") ' get new part
             rtHistory.AppendRTItem rtComment
' append new part
             rtHistory.AddNewLine
2 ' blank lines
             rtHistory.AppendRTItem rtTemp
' append old report from rtTemp
             wipeRT doc,
"convAddReportTemp" ' wipe temp
             wipeRT doc,
"convAddPart" ' wipe comment
             rtHistory.Update
' process all updates
             rtHistory.Compact
' end of edits, compact
             
Call doc.ComputeWithForm(True, False)
             
Call doc.Save(True, False)
     
End If ' mustAddPart
     strDocid = doc.UniversalID   ;
' Get UNID
     mustAddPart = Source.EditMode
' get editmode
     
Call Source.Close
     ws.EditDocument mustAddPart, db.GetDocumentByUnid(strDocid), , ,
False
End Sub
' PostSave
%REM
     Sub wipeRT
     wipe RT field
%END REM

Sub Sub wipeRT(doc As NotesDocument, rtName As String)
     doc.ReplaceItemValue rtName,
"" ' OverWrite with a simple text field
     doc.RemoveItem rtName

End Sub
' PostSave

How does it work:

First, save the existing RichText to a temp RichText field.  rtTemp has a copy of the rtHistory, with references to any $File items holding the attachments.

Then I use wipeRT to replace the existing RT field with a simple text field, which leaves the $File items intact.

Then I use a helper routine to add a bold header with a timestamp and the user, and append the comment.  Any $File references are copied.

wipeRT is used to get rid of the temp field and empty the Comment field without losing the $File attachments.  If you call NotesRichTextItem.Remove directly, those $File items get destroyed, leaving a nice icon image in the RichText field, but no content.


Have fun!

Recent Entries

    Archives