Insert RichText into RichText with LotusScript – Chapter 2

In an earlier post I wrote about how to insert richtext from another document into a specific position in an exiting richtext item using DXL. This solution works fine but has a few limitations. One of this limitations is the handling of attachments and embedded objects.

So I was looking for another solution. I tried to use LotusScript but due to some missing methods there is no way to achive the goal.

Then I searched the C++ API and found

LNSTATUS Insert( const LNRichText &richtext, LNRTCursor *cursor )

This was exactly what I was looking for. I wrote a small console application to test this method. The function did exactly what it is supposed to do.

Using the

LNSTATUS GotoFirst( const LNString &searchstring )

method, it is possible, to insert the richtext at a specific position that can be defined by a placeholder. The placeholder can be of any name.

Insert Richtext - the placeholder

To delete the placeholder before inserting the replacing content, I use

rtTarget.Delete( &cursor, sizeof( InsertionPoint ));

I guess this is what IBM does when you use the FindAndReplace method of the NotesRichTextRange class. But with this metod you can replace text only. Replacing a string with richtext is not supported. I wonder, why IBM does not enhance this method. In my oppinion this is not very hard to do; as I stated before, the Notes API already has this functionality.

After a few more tests with attachments and embedded objects, I put all this stuff into an DLL. Now the function can be used from inside LotusScript. I decided to provide this new functionality as an DLL because all of my attempts to create an LSX ( using the LSX toolkit ) constantly crashes my client. Even with some help from Bill Buchan and Benjamin Langhinrich I was not able to build a stable function. Maybe i will succeed some day …

To use the InsertRichTextItem function from LotusScript, you have to copy “rt.dll” to your notes executable directory. Since the function uses the C++ API, you have to copy “lcppn70.dll”, too. I have created and tested the function with Notes 7.0.1, but it should work in former versions of Notes as well.

In the Declaration section of your Lotus Script put the following lines

Declare Function InsertRichTextItem Lib "rt" (_
Byval dbServer As String,_
Byval dbPath As String,_
Byval lngSourceNoteID As Long,_
Byval strSourceRTField As String,_
Byval lngTargetNoteID As Long,_
Byval strTargetRTField As String,_
Byval InsertionPoint As String) As String

The function is called as follows

strRet = InsertRichTextItem ( DB_SERVER, DB_FILE,  SourceNOTEID, "Body", TargetNOTEID, "Body","")

The function returns “OK” when completed successfully, otherwise it throws an error.

Sub Click(Source As Button)

	Dim s As New NotesSession
	Dim db As NotesDatabase
	Dim col As NotesDocumentCollection
	Dim doc As NotesDocument

	Dim SourceDoc As NotesDocument ' contains the richtext to be inserted
	Dim TargetDoc As NotesDocument ' insert RT here

	Dim rtItem As NotesRichTextItem
	Dim retval As Integer

	Set db = s.GetDatabase( DB_SERVER, DB_FILE)
	Set col = db.AllDocuments
	Set SourceDoc = col.GetNthDocument(2) ' SecondDocument
	Set TargetDoc = col.GetNthDocument(1) ' FirstDocument

	Dim SourceNOTEID As Long
	Dim TargetNOTEID As Long

	' Convert NOTEID from HEX to Long
	TargetNOTEID = Val("&H" + TargetDoc.NoteID)
	SourceNOTEID = Val("&H" + SourceDoc.NoteID)

	Msgbox InsertRichTextItem ( DB_SERVER, DB_FILE,  SourceNOTEID, "Body", TargetNOTEID, "Body","")

End Sub

Attached you’ll find a sample database. The necessary files are included in the “first document” – doc.

If you find this function useful, pls. let me know.

Technorati: