Why does editing one RT field via Lotusscript affect the other RT field?

Domino so frustrates me sometimes! What is up with this “feature”?

I have two (actually more) dynamic tables on a form. They are created using hidden, multi-value fields which store the columns values. Then, if a user adds, modifies, or deletes, a row, Lotusscript deletes the richtext and creates a new table with the new values, then it saves, closes, and re-opens the document. See: http://tinyurl.com/2cqqbo

So, to clarify, I have two “tables” which use the following multivalue fields to store the columns’ values:

Table 1, column 1: PM

Table 1, column 2: PM_Title

Table 2, column 1: IM

Table 2, column 2: IM_Title

Then, I have the following computed, richtext fields where the tables are generated:

Table 1: PM_Table

Table 2: IM_Table

If the user clicks a button to add (or modify or delete) a row to the PM table, then a dialogbox collects the values and adds (or modifies or deletes) the values in the PM and PM_Title fields and calls Sub generateTable().

Sub generateTable() deletes the item named PM_Table, saves the document, re-creates PM_Table, adds a table to it, and populates the table with values from PM and PM_Title. Then it re-saves the document, closes it, and reopens it.

When the document is reopened, the table in IM_Table is changed. PM_Table comes out correctly, but IM_Table is all screwed up. The table in IM_Table has lost itsformatting, margins and column widths.

What’s up with that? Why is changing the content of one RT field affecting the others? This is not supposed to happen!

Please help!

Thanks,

-Jeff

Subject: Why does editing one RT field via Lotusscript affect the other RT field?

Jeff,

Are PM_Table and IM_Table saving to different NotesRichTextItems? Can you post the code?

Personally I would not use the NotesRichTextNavigator and NotesRichTextTable objects to create tables in a NotesRichTextItem. I would create a form that has the fields I am looking to put into the richtextitem and then use the NotesDocument.RendertoRTItem method to create my table in my richtext field. It is so much less of a pain in the ass that using the RichText objects and has the added benefit of allowing you to nest table, specify fields and so on.

Still, if you want to go the richtext route, post the code and let’s see what we can do.

Brandt

Subject: RE: Why does editing one RT field via Lotusscript affect the other RT field?

PM_Table and IM_Table save to different NotesRichTextItems.

I will post a condensed version of the code. I’d like to get this method working since I understand it and just spent two days on working on it; but could you post a simple example of the RenderToRTItem method you describe for me to examine? I don’t see how I could make a varying length table using it.

Thank you very much if you can figure out what’s wrong.

-Jeff

Here is my code:

Two hot spots:

Sub Click(Source As Button)

'Project Management add resources button.

Call addResource("PM")

End Sub

Sub Click(Source As Button)

'Interactive Marketing add resources button.

Call addResource("IM")

End Sub

I have other hotspots for changeResource and removeResource, to change or delete a row, but I won’t post them here (unless you want 'em). The rest of this code isn’t as complicated as it is long, addResource() merely adds a row to the end of two column fields, GetCurrentFields() and SetTableFields() are simple utilities, and finally generateTable() creates the new table in the correct richtext item. generateTable() is where the problem (if I’ve caused it) or bug (if it’s a Lotus bug) exists.

Sub addResource(field As String)

'Get resource name and title from dialog box, put it in the resource and resource_title fields (or items).

Dim doc As NotesDocument, TempDoc As NotesDocument

Dim itmResource As NotesItem, itmTitle As NotesItem

Dim varResource As Variant, varTitle As Variant

Dim pos As Integer



'Get the current fields

Set doc = ws.CurrentDocument.Document

Set TempDoc = db.CreateDocument

Call GetCurrentFields(doc, field, itmResource, itmTitle, varResource, varTitle)



'Get the vals for the new row

If Not ws.DialogBox( "TablePop", True, True, False, False, False, False, "Add Resource", TempDoc, True, False) Then Exit Sub



'Add the new vals to the ends of the arrays.

If (Ubound(varResource)) = 0 And Fulltrim(varResource(0)) = "" And Fulltrim(varTitle(0)) = "" Then

	pos = 0

Else

	pos = Ubound(varResource)+1

End If

Redim Preserve varResource(pos)

Redim Preserve varTitle(pos)

Call SetTableFields(TempDoc, doc, itmResource, itmTitle, varResource, varTitle, pos)



'Save new doc and redraw it

Call doc.save(True,False,True)

Call generateTable (field, itmResource, itmTitle)

End Sub

Sub GetCurrentFields(doc As NotesDocument, field As String, itmResource As NotesItem, itmTitle As NotesItem, varResource As Variant, varTitle As Variant)

'Gets item and array of values of current table fields on main doc

Set itmResource = doc.GetFirstItem(field)

Set itmTitle = doc.GetFirstItem(field & "_Title")



varResource = itmResource.Values

varTitle = itmTitle.Values

End Sub

Sub SetTableFields(TempDoc As NotesDocument, doc As NotesDocument, itmRes As NotesItem, itmTitle As NotesItem, varRes As Variant, varTitle As Variant, pos As Integer)

'Sets the table fields on the main document

Dim resName As String

resName = TempDoc.Resource(0)

If Fulltrim(resName) = "" Then resname = "(to be determined)"

varRes(pos) = resName

varTitle(pos) = TempDoc.Title(0)



'Reset the fields on the main document

Set itmRes = doc.ReplaceItemValue(itmRes.Name, varRes)

Set itmTitle = doc.ReplaceItemValue(itmTitle.Name, varTitle)

End Sub

Sub generateTable (field As String, itmResource As NotesItem, itmTitle As NotesItem)

%REM


Called by: Called from the three different links click event (add, modify and delete links)

Purpose: Basically redraws the table each time the code in the links is fired.


%END REM

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

Set uidoc = ws.CurrentDocument

Set doc = uidoc.Document



'Checks to see if the Body field is in the document, if there is already a Body field, discard it. 

Dim RtItem As NotesRichTextItem, rtitmName As String

rtitmName = field & "_Table"



If doc.HasItem(rtitmName) Then		doc.RemoveItem(rtitmName)

Call doc.save(True,True)

If itmResource.Values(0) = "" Then		Exit Sub

Set RtItem = New NotesRichTextItem(doc, rtitmName) 



'For this example, we only have 2 columns.

'The rows are determined by the number of values in the column1 multivalue field + 2 to to accomodate for the header row of the table.

Dim cols As Integer

Dim rows As Integer

cols = 2

rows = Ubound(itmResource.Values) + 2



'Create a NotesRichTextStyle so we can set the fonts and stuff

'We will be using .NotesFont, FontSize, NotesColor, Bold, Italic, Underline properties

Dim whiteRichStyle As NotesRichTextStyle

Dim blackRichStyle As NotesRichTextStyle

Dim highlightedRichStyle As NotesRichTextStyle



Set whiteRichStyle = s.CreateRichTextStyle

whiteRichStyle.NotesFont = RtItem.GetNotesFont("Default MultiLingual",True)

whiteRichStyle.FontSize = 9

whiteRichStyle.NotesColor = COLOR_WHITE

whiteRichStyle.Bold = True



Set blackrichStyle = s.CreateRichTextStyle

blackrichStyle.NotesFont = RtItem.GetNotesFont("Default MultiLingual",True)

blackrichStyle.FontSize = 9

blackrichStyle.NotesColor = COLOR_BLACK

blackrichStyle.Bold = False



Set highlightedRichStyle = s.CreateRichTextStyle

highlightedRichStyle.NotesFont = RtItem.GetNotesFont("Default MultiLingual",True)

highlightedRichStyle.FontSize = 9

highlightedRichStyle.NotesColor = COLOR_RED

highlightedRichStyle.Bold = False



'Create a NotesRichTextParagraphStyle object so we can manipulate properties of the table we are going to create.

'Some of the properties we will be using are: LeftMargin,RightMargin and Alignment

Dim tableColStyles(1 To 2) As NotesRichTextParagraphStyle



Dim i As Integer



'Setting the width of each column using the NotesRichTextParagraphStyle we created

For i = 1 To 2

	Set tableColStyles(i) = s.CreateRichTextParagraphStyle

	tableColStyles(i).FirstLineLeftMargin = 0

	tableColStyles(i).LeftMargin = 0

	Select Case i

	Case 1

		tableColStyles(i).RightMargin = RULER_ONE_INCH * 2.5		'Ruler_One_Inch - constant

	Case 2

		tableColStyles(i).RightMargin = RULER_ONE_INCH * 2.6

		tableColStyles(i).Alignment = ALIGN_RIGHT

	End Select

Next i



'Now that we have instructed notes how we want the table built. We create the table w/a AppendTable method of the NotesRichText class

Call RtItem.AppendTable(rows, cols,,,tableColStyles)

Call doc.save(True,False)



'We create a RichTextNavigator so we can move around in the Richtext Item

Dim RtNav As NotesRichTextNavigator

Dim RtTable As NotesRichTextTable

Set RtNav = RtItem.CreateNavigator



'Checks to see if a table is in the Richtext field. If not then exit sub..

If Not RtNav.findFirstElement(RTELEM_TYPE_TABLE) Then		Exit Sub



'From the RichTextNavigator object we can get a handle to the Table.

Set RtTable = RtNav.getelement



'If you look in the help the .style property of the NotesRichTextTable lets you set the colors

'of the table cells, same properties you set if you create a table and click on the color tab

'and pick a table color style.

RtTable.Style = TABLESTYLE_TOP

Dim colorObject As NotesColorObject

Set colorObject = s.CreateColorObject



'You could use colors like COLOR_BLUE if you want one of the 16 normal colors.

'I wanted to use the blue generated w/the rgb colors 43,93,159, so I used the SetRGB to do this

’ Call colorObject.SetRGB(43,93,159)

colorObject.NotesColor = COLOR_GRAY

Call RtTable.SetColor(colorObject)

colorObject.NotesColor =  COLOR_WHITE

Call RtTable.SetAlternateColor(colorObject)

Call RtNav.findFirstElement(RTELEM_TYPE_TABLECELL)



'Applied the whiteRichStyle to richtext item (this the is for the header row)

Call RtItem.AppendStyle( whiteRichStyle  )

Call RtItem.begininsert(rtNav)

Call RtItem.AppendText("Resource")

Call RtItem.endinsert

Call RtNav.findnextelement(RTELEM_TYPE_TABLECELL)

Call RtItem.begininsert(rtNav)

Call RtItem.AppendText("Title")

Call RtItem.endinsert

Call RtItem.AppendStyle( blackRichStyle  )



'Here is where I write the data into the table cells w/data that is stored in my multivalue fields

'Resource and Title. These fields are populated by use of dialogbox that is called

'in the Add link code

For i = 0 To Ubound(itmResource.Values)

	Call RtNav.findnextelement(RTELEM_TYPE_TABLECELL)

	Call RtItem.begininsert(rtNav)

	If itmResource.Values(i) = "(to be determined)" Then

		Call RtItem.AppendStyle( highlightedRichStyle  )

		Call RtItem.AppendText(itmResource.Values(i))

		Call RtItem.AppendStyle( blackRichStyle  )

	Else

		Call RtItem.AppendText(itmResource.Values(i))

	End If

	Call RtItem.endinsert

	Call RtNav.findnextelement(RTELEM_TYPE_TABLECELL)

	Call RtItem.begininsert(rtNav)

	Call RtItem.appendtext(itmTitle.Values(i))

	Call RtItem.endinsert

Next i

Call doc.save(True,True)



Call uidoc.fieldsettext("SaveOptions","0")

uidoc.close



Call ws.editdocument(True,doc)

Set uidoc = ws.currentdocument

End Sub

Subject: RE: Why does editing one RT field via Lotusscript affect the other RT field?

Jeff,

Where is the addResource code contained? is it in a script library or otherwise stored in the globals section of your form? I am trying to figure out the scope of your varResource and varTitle variables. It doesn’t look like there is anywhere that you clear those variables when running the code, and I am wondering if every time you have tested this if you clicked the one action and then the other and if you would have the issue reversed if you clicked the buttons in reverse order. If the variables are global to the form, they would retain the values from the first action being run, and then when you call Redim Preserve you would preserve the earlier action’s variable values and just add to the array. It would explain the strange behavior.

When I mentioned RendertoRTItem earlier, what i was suggesting is that you create a form that contains the table as you want it, with fields contained in the table. Then when you go to create your table in the richtext field you can take your doc object, create a copy of it with all of your items in it and then set the form equal to the form that contains your table. When you call RenderToRTItem, notes takes a picture of that doc and puts it in the RichText field. That way, you don’t have to screw with using RTNavigators and all of that.

Check out this link:

http://www.scottgood.com/jsg/blog.nsf/d6plinks/SGOD-6T4PZV

Scott gave this demo at a NE Ohio Lotus Users Group meeting and he discussed how to effectively use RendertoRTItem. It has totally kept me from chasing building a table via code, which, as you can see, is an utter morass.

Anyway, even if you do want to go the RenderToRTItem route, I would like to know if those variants are global variables.

hth.

brandt

Subject: RE: Why does editing one RT field via Lotusscript affect the other RT field?

addResource is its own module, hanging off of the “(Globals)Status” tree of the form. All of the subs shown are in the Globals section (except for the two hotspots, which I’ll bet you can guess where they come from).

varResource and varTitle are declared in addResource and passed around to all the other Subs. They are valued anew in Sub GetCurrentFields. (Also, since the document closes and reopens after each resource being added, varResource and varTitle would always start as blank.)

Even if they weren’t empty, the problem is not that the wrong information is being entered into other richtext fields. The problem is that the other richtext fields’ tables are losing their formatting. In other words, when I click on the the Call addResource(“PM”) hotspot, the table added to IM_Table is losing its formatting.

I tried modifying addResource() to set those fields to empty strings at the start each time, but still had the same problem.

Do you have a direct link to Scott’s example. I can’t find it (I’m not sure what to look for) from the link you gave.

I’ll have to come back to this tomorrow, but I anxiously await your reply since I really, really need this answer.

Thanks (again),

-Jeff

Subject: RE: Why does editing one RT field via Lotusscript affect the other RT field?

Jeff,

Here’s a link to the .zip

http://www.scottgood.com/jsg/blog.nsf/2/SGOD-6T4Q3F/$File/25ThingsToDo.zip

feel free to e-mail me at bfundakATolysteel.com. I may have some examples I can share with you.

Brandt