Some time ago I wrote a small LotusScript agent, that updates a document's NotesRichTextItem with text, that I send to my Notes mailfile. Problem is: I wanted to insert the text at the *beginning* of the Body item, what seems simple and still has some pitfalls. Here is what I tried, and the solution:
I started out using the NotesRichTextNavigator and the BeginInsert method. Worked fine - until I had an embedded picture in my item; after the update, the picture showed as a black square (ouch).
Next try: Copying the Body item into a temporary NotesRichTextItem, emptying the Body item, writing my text, and then appending the temporary item (appendRTItem). My problem was, that I wanted to leave the temporary item as a backup. In my local tests, this worked perfectly, until I changed the agent settings to "triggered" - which resulted in the error "Notes error: Attachment found on more than one document". The reason obviously is, that after copying the item I had the same attachments in both the Body and the BodyTmp item. Weird, but explainable with a little good-will. What is really surprising is, that this error only occurs in an event triggered agent, not in a manually started agent (did some research after that). The error occurs on the document.save method.
Solution: You have to remove the temporary item before saving the document (I used a different way for my backup then).
Here is the (shortened) code (removed the dims):
Set rtBody = doc.GetFirstItem("Body") ' get the target NotesRichTextItem
Set rtBodyTmp = doc.CopyItem(rt, "Body") ' copy it temporarily
Call rtBody.remove() ' remove the target NotesRichTextItem to "start from scratch"
Set rtBody = New NotesRichTextItem(doc, "Body") ' create a new Body item
Call rtBody.AppendText(myText) ' insert text at top (as the item is empty)
Call rtBody.AppendRTItem(rtBodyTmp) ' now add the former content
Call rtBodyTmp.remove() ' important for triggered (!) agents: remove the temporary item
Call doc.Save(True, False, False)
About the Author
Marcus Foerster works as Systems Architect for Pentos AG in Munich, Germany. His focus lies on the application side, creating collaboration systems for users, groups and enterprises to get their daily work done. This approach includes integrating complex workflows with intuitive user interfaces, using Lotus Notes/Domino with Adobe Flex and other Web technologies.
Read more in Marcus' blog: http://marcus.foerster.com/blog