Genii Weblog

Midas Tip #2: Inserting in reverse order, or other methods

Mon 10 Sep 2007, 10:09 AM



by Ben Langhinrichs
As I mentioned in my first tip today, I wanted to insert some text at the beginning of a message.  It turns out that with the Midas chunk model, this is a bit more difficult than appending at the end.  The reason is simple, but perhaps more clear if I write in pseudo-code:

[Text 1 ==>]This is the first line of my rich text, and it can go on until a paragraph break, [Text 2 ==>]or until a font or style change[Text 3 ==>], which breaks the text into multiple records.

The text above represents a paragraph, with each text record identified.  Now, let's try and insert the following

Subject: A fairly meaningless paragraph

If we go with the intuitive choice, we would have:

Set rtchunk = rtitem.DefineChunk("Text 1")
Call rtchunk.InsertText("Subject: ", "+Bold -Italic 10pt")
Call rtchunk.InsertText(doc.Subject(0)+Chr(0), "+Bold -Italic 10pt")

but let's look at what the result after each line of code

Set rtchunk = rtitem.DefineChunk("Text 1")

[Text 1 ==>]This is the first line of my rich text, and it can go on until a paragraph break, [Text 2 ==>]or until a font or style change[Text 3 ==>], which breaks the text into multiple records.

Call rtchunk.InsertText("Subject: ", "+Bold -Italic 10pt")

[Text 1 ==>]Subject: [Text 2 ==>]This is the first line of my rich text, and it can go on until a paragraph break, [Text 3 ==>]or until a font or style change[Text 4 ==>], which breaks the text into multiple records.

Call rtchunk.InsertText(doc.Subject(0)+Chr(0), "+Bold -Italic 10pt")

[Text 1 ==>]A fairly meaningless paragraph
[Text 2 ==>]Subject: [Text 3 ==>]This is the first line of my rich text, and it can go on until a paragraph break, [Text 4 ==>]or until a font or style change[Text 5 ==>], which breaks the text into multiple records.

This obviously ins't what we want, as the subject goes before the word "Subject:".  The problem is, as soon as we insert the text, the newly inserted text becomes Text 1.  The easiest solution, although it looks odd in the code, is to add the desired text in reverse:

Set rtchunk = rtitem.DefineChunk("Text 1")
Call rtchunk.InsertText(doc.Subject(0)+Chr(0), "+Bold -Italic 10pt")
Call rtchunk.InsertText("Subject: ", "+Bold -Italic 10pt")

[Text 1 ==>]Subject: [Text 2 ==>]A fairly meaningless paragraph
[Text 3 ==>]This is the first line of my rich text, and it can go on until a paragraph break, [Text 4 ==>]or until a font or style change[Text 5 ==>], which breaks the text into multiple records.

There are other ways, such as inserting a table and adding content to that, or adding something fixed, such as an anchor, and then inserting before that so that it never shifts, but I want to make clear that this is not a bug, but a feature.  (You knew that, right?)  Of course, it is also why OpenSesame offers a chunk which can become a cursor.  We have to learn, don't we?

Copyright © 2007 Genii Software Ltd.

What has been said:

No documents found