Problems to replace string in a multi Body rich text field

Hi everybody,

I’m having problems to replace a substring inside a rich text field called BODY. This works normally. The problem appears when my field BODY exists several times in a DOC.

I don’t know if I could explain, so in another words, my document properties (at Notes Client) shows me that the document has more than one field called BODY (probably because the field is too large, that the Notes broke the information in two or more pieces, using the same field name).

I’m trying to access the second piece of information, but I’m not having success. Normally, after I run my code, the document ALWAYS lose the extras-fields of BODY. The document stays with only ONE field (losing all the rest of information).

Anyone know how can I replace a substring inside multi-field (the same field broken in many pieces)?

Thanks a lot!

My code is:

Sub Initialize

On Error Goto ErrorHandler



Dim ss As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Dim buffer As String

Dim array As Variant

Dim i As Integer

Dim y As Integer



Set db = ss.CurrentDatabase

Set view = db.GetView("vdoc")

Set doc = view.GetFirstDocument

i = 0



While Not doc Is Nothing 

	If doc.HasItem("Body") Then

		

		buffer = ""

		array = doc.GetItemValue("Body")

		y = 0

		Forall x In array

			buffer = DSStringReplaceSubstring(Cstr(x), "filesdc01", "qualitaalias") 

			array(y) = buffer

			y = y + 1

		End Forall

		

		Call doc.ReplaceItemValue("Body", array)

		Call doc.Save(True, False)

	End If

	

	i = i + 1

	Print "Checked: " & Cstr(i)

	Set doc = view.GetNextDocument(doc)

Wend



Exit Sub

ErrorHandler:

Msgbox "Error: " & Err() & ": " & Error() &  " na linha " & Erl()	

End Sub

Subject: Problems to replace string in a multi Body rich text field

Hey there Andre.It looks to me like you are attempting to treat/process the BODY field as though it were a simple text list (multi-value text field). Because the field is RICHTEXT, your code simply will not work properly.

I would suggest that you make use of the NotesRichTextNavigator and NotesRichTextRange classes to do this. Both are fairly well documented in Designer Help.

I realize this isn’t a turnkey solution, but it should get you pointed in the right direction.

Hope this helps!

-Devin.

Subject: Problems to replace string in a multi Body rich text field

Hello Andre,

Devin has completely right. The only way to get the content of a richtext field in backend is the NotesRichTextNavigator. I had the same problem short time ago.

The simple example is:

SOLUTION:

(get the content of a richtext field in backend if content is greater than 32k and Notes increased the field name to hold the value in e.g. (Body, Body, Body, Body, …)

Sub GetRichTextContent(doc As NotesDocument, db As NotesDatabase, session As NotesSession)

Dim RText As NotesRichTextItem

Dim RTNav As NotesRichTextNavigator

Dim RTRange As NotesRichTextRange

Dim count As Integer

Dim TextString As String

If doc.HasItem(“Body”) Then

Set RText = doc.GetFirstItem(“Body”)

If (RText.Type = RICHTEXT) Then

Set RTNav = RText.CreateNavigator

Set RTRange = RText.CreateRange

If RTNav.FindFirstElement(RTELEM_TYPE_TEXTPARAGRAPH) Then

count% = 0

Do

count% = count% + 1

Call RTRange.SetBegin(RTNav)

TextString$ = TextString$ + RTRange.TextParagraph

Loop While RTNav.FindNextElement(RTELEM_TYPE_TEXTPARAGRAPH)

End If

End If

End If

End Sub

Hope it helps other developers with the same “problem”,

Matthias

Subject: Problems to replace string in a multi Body rich text field

Andre,

It would help to know how the documents are generated initially in your database. Notes/Domino will not arbitrarily break up a rich text field into multiple rich text fields of the same name. More likely than not, the multiple fields of same name are being generated when the document is created.

However, the reason the values of the fields are being blown out when you run this code is because you use the ReplaceItemValue method of the NotesDocument object. According to the Notes help ReplaceItemValue does this:

Replaces all items of the specified name with one new item, which is assigned the specified value. If the document does not contain an item with the specified name, the method creates a new item and adds it to the document.

In your code you look to see if the doc object hasItem Body. When it finds the first instance of Body when you do the GetItemValue, it will not bother with the others. Later, you call ReplaceItemValue and it will replace ALL instances, thus creating the behavior you describe.

What you want to do instead is loop through all NotesItems like this:

Forall Item in doc.Items

   If Item.Name = "Body" then

            ...set values of your array here...

   End If

End Forall

However, you are going to run into two more issues here. First, once you set your variant variable equal to the first body item, you will need to redim the variable to accept the new values as you loop through the NotesItems on the doc object. But since we know that the NotesItem and GetItemValue properties return strings or string arrays, it is better to test to make sure the body field is a rich text field and then dump the converted string into a string array like so.

Dim strArray() as String

Redim strArray(0)

Forall Item in doc.Items

   If Item.Name = "Body" and Item.Type = RICHTEXT then

            If strArray(0) = "" then

                     strArray(0) = Item.Values

            Else

                     Redim Preserve strArray(Ubound(strArray) + 1) <--add another element to the array list

                     strArray(Ubound(strArray)) = Item.Values

            End If

    End If

End Forall

Once you have created the strArray like this, you will have all of the values of the Body fields. Then you can go ahead and make your changes to the strings and blow out the Body fields with ReplaceItemValue.

hth.

brandt

Subject: Multiple Rich Text Items

RE: “…Notes/Domino will not arbitrarily break up a rich text field into multiple rich text fields of the same name…”

Sorry Brandt, but you are mistaken.

Notes most definately will break up a rich text field into multiple RichTextItems of the same name; that is core to it’s functionality. The reason for this is that each indivdual item has a maximum size limit -yet you can put very large amounts of information into a Rich Text field. Notes breaks up this information into as many individual items as necessary.

However this normally isn’t a problem the NotesDocument.GetFirstItem function returns the first one of these items. When you are working with a rich text item, and you use the NotesRichTextNavigator class, you don’t have to worry about which of these individual items you are working with -the navigator handles all of them as a single item. Example:

(assuming a NotesDocument ndoc already exists)

Dim myRT As Variant

Dim myRTnav As NotesRichTextNavigator

If Not ndoc.HasItem(“Body”) Then Exit Sub ’ bail out, item doesn’t exist.

Set myRT = ndoc.GetFirstItem(“Body”)

If Not (myRT.Type = RICHTEXT) Then Exit Sub ’ bail out, item isn’t rich text

Set myRTnav = myRT.CreateNavigator

’ now use the navigator to navigate the NotesRichTextItem

Hope this helps!

-Devin.

Subject: RE: Multiple Rich Text Items

Devin,

It may indeed break up the rich text field (I am not aware of how it works under the hood, but have never seen the behavior he mentions myself–merely $FILE fields for attachments, etc.), but you are needlessly complicating things by adding a NotesRichTextNavigator. He doesn’t care about anything other than text. If he accesses the values the way I showed him, NotesItem.Values will take the text and convert it in to plain text, which is what he is looking to do. The code I gave him will do what he wants, without having to get a RichTextNavigator involved.

According to the Help:

The data type depends upon the type of the item.

For reading, the data type of the item determines the data type of Values:

Item type	Values data type

Rich text	String. The text in the field, rendered into plain text.

So yes, I was ignorant of how a rich text field works beneath the hood, but what I gave him should do exactly what he wants to do.

brandt

Subject: RE: Multiple Rich Text Items

Point well taken. (I hope I didn’t come off sounding like an ass -it wasn’t my intention).

What happens if he has stuff other than just text in there (attachments, images, etc)? Will the NotesItem.Values ignore these?

-Devin.