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