Normally, updating RichTextFields in an opened IBM Notes document requires saving, closing and then reopening the document. If you use the RichTextField to attach a file to the document even after doing all this closing and reopening, it is not guaranteed that the attachment is inserted where you want it to be. Another problem that is implicated with this is the necessity to save documents you do not want to save, yet.
Fortunately, there is an undocumented method in LotusScript that enables us to update RichTextFields on unsaved UIDocuments without the need for saving and reopening them.
It's called
NotesUIDocument.ImportItem( tmpDoc As NotesDocument, RichTextItem As NotesRichTextItem)
and it is part of in the NotesUIDocument class.
Explanation standard method:
The standard method to embed files or document links etc. into RichTextFields is the use of
NotesRichTextItem.embedObject
Example Code:
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Set doc = ws.CurrentDocument.Document
Set rtitem = New NotesRichTextItem(doc, "RichTextField")
Call rtitem.EmbedObject( EMBED_ATTACHMENT, "", "C:\Desktop\Document.docx")
Fig. 1: Notes form with buttons for the standard method and our workaround for attaching files in the RichTextField
After using the standard method the problems stated above become obvious. When the document is saved and reopened the file is attached at the end of the document, but not in the RichTextField where it should be.
Fig. 2: Result of the standard method EmbedObject of LotusScript
Explanation workaround:
Now we will use the ImportItem method.
Example Code:
Dim sess As New NotesSession
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim tmpdoc As NotesDocument
Dim rtItem As NotesRichTextItem
Dim uiDoc As NotesUIDocument
'uiDoc contains the RichTextField we want to import our file into
Set uiDoc = ws.CurrentDocument
'Create a support NotesDocument (tmpDoc)
Set db = sess.currentDatabase
Set tmpdoc = db.CreateDocument
'Create a RichTextItem on the support document to embed the desired file into
Set rtItem = New NotesRichTextItem(tmpdoc, "RichTextField")
'embed the desired file into the created RichTextItem
Call rtItem.EmbedObject( EMBED_ATTACHMENT, "", "C:\Desktop\Document.docx")
Call rtItem.update
'!!Using computeWithForm ensures that the attachment is completely embed into the RichTextField before
'we try to import it into our uiDoc. It works even though we have no form specified!!
Call tmpdoc.ComputeWithForm(False, False)
'Put Cursor into the RichTextField --> precondition for the file's import
Call uiDoc.GoToField("RichTextField")
'finally import the desired file into the RichTextField on the uiDoc.
Call uiDoc.ImportItem(tmpDoc, "RichTextField")
Fig. 3: Result of the workaroung method ImportItem
As you can see, right after using the workaround button, the file is attached into the RichTextField of our opened document as desired. No saving and reopening needed!
!Please keep in mind that this workaround method is undocumented an could be removed from LotusScript by IBM!
I hope this workaround helped you as much as it helped me!
If you have any questions or suggestions do not hesitate to contact us.