Creating a dynamic table using lotusscript

I am trying to create a dynamic table using the following code


Sub CreateTable

Dim sess As New NotesSession

Dim ws As New NotesUIWorkspace

Dim dbCurr As NotesDatabase

Dim docProcess As NotesDocument

Dim dcProcess As NotesDocumentCollection



Dim rtiItem As NotesRichTextItem



Dim rtnav As NotesRichTextNavigator

Dim rtt As NotesRichTextTable

Dim rtsTableHeader As NotesRichTextStyle

Dim rtsTableRow As NotesRichTextStyle



Dim sData(26) As String

Dim sHeaders(26) As String

Dim sPrefix As String

Dim iCtr As Integer

Dim iRow As Integer

Dim iCol As Integer

'We declare an array of paragraph styles to pass in when creating the table

Dim rtpsCols(26) As NotesRichTextParagraphStyle



Set dbCurr = sess.CurrentDatabase





Set dcProcess =dbCurr.AllDocuments



If dcProcess.Count > 0 Then

'These are display headers for the table

	sHeaders(0) = "Header 1"

	sHeaders(1) = "Header 2"

	sHeaders(2) = "Header 3"

	sHeaders(3) = "Header 4"

	sHeaders(4) = "Header 5"

	sHeaders(5) = "Header 6"

	sHeaders(6) = "Header 7"

	sHeaders(7) = "Header 8"

	sHeaders(8) = "Header 9"

	sHeaders(9) = "Header 10"

	sHeaders(10) = "Header 11"

	sHeaders(11) = "Header 12"

	sHeaders(12) = "Header 13"

	sHeaders(13) = "Header 14"

	sHeaders(14) = "Header 15"

	sHeaders(15) = "Header 16"

	sHeaders(16) = "Header 17"

	sHeaders(17) = "Header 18"

	sHeaders(18) = "Header 19"

	sHeaders(19) = "Header 20"

	sHeaders(20) = "Header 21"

	sHeaders(21) = "Header 22"

	sHeaders(22) = "Header 23"

	sHeaders(23) = "Header 24"

	sHeaders(24) = "Header 25"

	sHeaders(25) = "Header 26"

	sHeaders(26) = "Header 27"

'An array of data for the Columns

	sData(0) = "Col 1"

	sData(1) = "Col 2"

	sData(2) = "Col 3"

	sData(3) = "Col 4"

	sData(4) = "Col 5"

	sData(5) = "Col 6"

	sData(6) = "Col 7"

	sData(7) = "Col 8"

	sData(8) = "Col 9"

	sData(9) = "Col 10"

	sData(10) = "Col 11"

	sData(11) = "Col 12"

	sData(12) = "Col 13"

	sData(13) = "Col 14"

	sData(14) = "Col 15"

	sData(15) = "Col 16"

	sData(16) = "Col 17"

	sData(17) = "Col 18"

	sData(18) = "Col 19"

	sData(19) = "Col 20"

	sData(20) = "Col 21"

	sData(21) = "Col 22"

	sData(22) = "Col 23"

	sData(23) = "Col 24"

	sData(24) = "Col 25"

	sData(25) = "Col 26"

	sData(26) = "Col 27"

'Set up the text styles we want to use for displaying the text

	Set rtsTableHeader = sess.CreateRichTextStyle

	rtsTableHeader.FontSize = 10 

	rtsTableHeader.Bold = True

	Set rtsTableRow= sess.CreateRichTextStyle

	rtsTableRow.FontSize = 8

	rtsTableRow.Bold = False

'Set up the paragraph styles for the table

'The column widths are set by setting the left margin to 0 then the right margin to

'what the column width should be

	Set rtpsCols(0) = sess.CreateRichTextParagraphStyle

'This column in left aligned

	rtpsCols(0).Alignment = 0

	rtpsCols(0).Firstlineleftmargin = 0

	rtpsCols(0).Leftmargin = 0

	rtpsCols(0).RightMargin = RULER_ONE_CENTIMETER * 7.51

'This column is centre aligned

	Set rtpsCols(1) = sess.CreateRichTextParagraphStyle

	rtpsCols(1).Alignment =3

	rtpsCols(1).Firstlineleftmargin = 0

	rtpsCols(1).Leftmargin = 0

	rtpsCols(1).RightMargin = RULER_ONE_CENTIMETER * 1.43  

	

	Set rtpsCols(2) = sess.CreateRichTextParagraphStyle

	rtpsCols(2).Alignment =3  

	rtpsCols(2).Firstlineleftmargin = 0

	rtpsCols(2).Leftmargin = 0

	rtpsCols(2).RightMargin = RULER_ONE_CENTIMETER * 2.18  

	

	Set rtpsCols(3) = sess.CreateRichTextParagraphStyle

	rtpsCols(3).Alignment =3

	rtpsCols(3).Firstlineleftmargin = 0

	rtpsCols(3).Leftmargin = 0

	rtpsCols(3).RightMargin = RULER_ONE_CENTIMETER * 1.68

	

	Set rtpsCols(4) = sess.CreateRichTextParagraphStyle

	rtpsCols(4).Alignment =0

	rtpsCols(4).Firstlineleftmargin = 0

	rtpsCols(4).Leftmargin = 0

	rtpsCols(4).RightMargin = RULER_ONE_CENTIMETER * 4.73

	

	Set rtpsCols(5) = sess.CreateRichTextParagraphStyle

	rtpsCols(5).Alignment =0

	rtpsCols(5).Firstlineleftmargin = 0

	rtpsCols(5).Leftmargin = 0

	rtpsCols(5).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	

	Set rtpsCols(6) = sess.CreateRichTextParagraphStyle

	rtpsCols(6).Alignment =0

	rtpsCols(6).Firstlineleftmargin = 0

	rtpsCols(6).Leftmargin = 0

	rtpsCols(6).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	

	Set rtpsCols(7) = sess.CreateRichTextParagraphStyle

	rtpsCols(7).Alignment =0

	rtpsCols(7).Firstlineleftmargin = 0

	rtpsCols(7).Leftmargin = 0

	rtpsCols(7).RightMargin = RULER_ONE_CENTIMETER * 3.73

	

	

	Set rtpsCols(8) = sess.CreateRichTextParagraphStyle

	rtpsCols(8).Alignment =0

	rtpsCols(8).Firstlineleftmargin = 0

	rtpsCols(8).Leftmargin = 0

	rtpsCols(8).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(9) = sess.CreateRichTextParagraphStyle

	rtpsCols(9).Alignment =0

	rtpsCols(9).Firstlineleftmargin = 0

	rtpsCols(9).Leftmargin = 0

	rtpsCols(9).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(10) = sess.CreateRichTextParagraphStyle

	rtpsCols(10).Alignment =0

	rtpsCols(10).Firstlineleftmargin = 0

	rtpsCols(10).Leftmargin = 0

	rtpsCols(10).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(11) = sess.CreateRichTextParagraphStyle

	rtpsCols(11).Alignment =0

	rtpsCols(11).Firstlineleftmargin = 0

	rtpsCols(11).Leftmargin = 0

	rtpsCols(11).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(12) = sess.CreateRichTextParagraphStyle

	rtpsCols(12).Alignment =0

	rtpsCols(12).Firstlineleftmargin = 0

	rtpsCols(12).Leftmargin = 0

	rtpsCols(12).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(13) = sess.CreateRichTextParagraphStyle

	rtpsCols(13).Alignment =0

	rtpsCols(13).Firstlineleftmargin = 0

	rtpsCols(13).Leftmargin = 0

	rtpsCols(13).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(14) = sess.CreateRichTextParagraphStyle

	rtpsCols(14).Alignment =0

	rtpsCols(14).Firstlineleftmargin = 0

	rtpsCols(14).Leftmargin = 0

	rtpsCols(14).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(15) = sess.CreateRichTextParagraphStyle

	rtpsCols(15).Alignment =0

	rtpsCols(15).Firstlineleftmargin = 0

	rtpsCols(15).Leftmargin = 0

	rtpsCols(15).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(16) = sess.CreateRichTextParagraphStyle

	rtpsCols(16).Alignment =0

	rtpsCols(16).Firstlineleftmargin = 0

	rtpsCols(16).Leftmargin = 0

	rtpsCols(16).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(17) = sess.CreateRichTextParagraphStyle

	rtpsCols(17).Alignment =0

	rtpsCols(17).Firstlineleftmargin = 0

	rtpsCols(17).Leftmargin = 0

	rtpsCols(17).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(18) = sess.CreateRichTextParagraphStyle

	rtpsCols(18).Alignment =0

	rtpsCols(18).Firstlineleftmargin = 0

	rtpsCols(18).Leftmargin = 0

	rtpsCols(18).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(19) = sess.CreateRichTextParagraphStyle

	rtpsCols(19).Alignment =0

	rtpsCols(19).Firstlineleftmargin = 0

	rtpsCols(19).Leftmargin = 0

	rtpsCols(19).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(20) = sess.CreateRichTextParagraphStyle

	rtpsCols(20).Alignment =0

	rtpsCols(20).Firstlineleftmargin = 0

	rtpsCols(20).Leftmargin = 0

	rtpsCols(20).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(21) = sess.CreateRichTextParagraphStyle

	rtpsCols(21).Alignment =0

	rtpsCols(21).Firstlineleftmargin = 0

	rtpsCols(21).Leftmargin = 0

	rtpsCols(21).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(22) = sess.CreateRichTextParagraphStyle

	rtpsCols(22).Alignment =0

	rtpsCols(22).Firstlineleftmargin = 0

	rtpsCols(22).Leftmargin = 0

	rtpsCols(22).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(23) = sess.CreateRichTextParagraphStyle

	rtpsCols(23).Alignment =0

	rtpsCols(23).Firstlineleftmargin = 0

	rtpsCols(23).Leftmargin = 0

	rtpsCols(23).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(24) = sess.CreateRichTextParagraphStyle

	rtpsCols(24).Alignment =0

	rtpsCols(24).Firstlineleftmargin = 0

	rtpsCols(24).Leftmargin = 0

	rtpsCols(24).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(25) = sess.CreateRichTextParagraphStyle

	rtpsCols(25).Alignment =0

	rtpsCols(25).Firstlineleftmargin = 0

	rtpsCols(25).Leftmargin = 0

	rtpsCols(25).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set rtpsCols(26) = sess.CreateRichTextParagraphStyle

	rtpsCols(26).Alignment =0

	rtpsCols(26).Firstlineleftmargin = 0

	rtpsCols(26).Leftmargin = 0

	rtpsCols(26).RightMargin = RULER_ONE_CENTIMETER * 2.18

	

	Set docProcess = dcProcess.GetFirstDocument

	While Not docProcess Is Nothing

'I’ve used good old Body as my rich text field

		Set rtiItem = docProcess.GetFirstItem("Body")

'The styles only have about three fonts that can be used by default

'Arial isn’t one of them so we have to ‘Get’ it from the rich text item

'I don’t really understand this but that’s how it works…

		rtsTableHeader.NotesFont = rtiItem.GetNotesFont ("Arial", True)

		rtsTableRow.NotesFont = rtiItem.GetNotesFont("Arial", True)

		   'We create the table, with 1 row, 5 columns, "" for the 

'title, so it’s not a tabbed table

'1440 is the margin in twips, which is an inch, or

'2.54*567(which is a cm in twips)

'rtpsCols is the array of rich text paragraph styles we

'set up earlier

		Call rtiItem.AppendTable(1,26,"",1440, rtpsCols)

		Call docProcess.Save(True,False)

'In order to get the table elements we have to use a

'couple of methods

'One is to get the table as a rich text table element

'The other is just to get navigate to the columns so we

'can insert text

'This element enables us to navigate through the rich text field

		Set rtnav = rtiItem.CreateNavigator

'Get the table as a rich text table element so we can add rows

		Set rtt = rtnav.GetFirstElement(RTELEM_TYPE_TABLE)

'Once you set a style the rich text field has that

'style until we say otherwise

		Call rtiItem.AppendStyle(rtsTableHeader)

'This navigates us to the first table cell

		Call rtnav.FindFirstElement (RTELEM_TYPE_TABLECELL) 

		For iCol = 1 To 26 Step 1

'Add in our text then get the next cell

			Call rtiItem.BeginInsert(rtnav)

			Call rtiItem.AppendText(sHeaders(iCol-1))

			Call rtiItem.EndInsert

			Call rtnav.FindNextElement (RTELEM_TYPE_TABLECELL)

		Next

'Now we change the style to our row style

		Call rtiItem.AppendStyle(rtsTableRow)

'Add a row, then go to the first cell in that row, which will be the next cell

		Call rtt.AddRow

		Call rtnav.FindNextElement (RTELEM_TYPE_TABLECELL)

		For iCtr=0 To 26

'Add in our text then get the next cell

			Call rtiItem.BeginInsert(rtnav)

			Call rtiItem.AppendText(sData(iCtr))

			Call rtiItem.EndInsert

			Call rtnav.FindNextElement (RTELEM_TYPE_TABLECELL)

		Next

		

		For m=0 To 15

			Call rtt.AddRow

			Call rtnav.FindNextElement (RTELEM_TYPE_TABLECELL)

			For iCtr=0 To 26

				Call rtiItem.BeginInsert(rtnav)

				Call rtiItem.AppendText(sData(iCtr))

				Call rtiItem.EndInsert

				Call rtnav.FindNextElement (RTELEM_TYPE_TABLECELL)

			Next

		Next

		

		Call docProcess.Save(True,False)

		Set docProcess = dcProcess.GetNextDocument(docProcess)

	Wend

End If

End Sub


The code it not throwing any error however the table is not getting created.

can someone please help me out with this.

Thanks

Subject: Creating a dynamic table using lotusscript

You need to update rtiItem

(rtiItem.Update)

Subject: RE: Creating a dynamic table using lotusscript

Might need to call rtt.Update. Should not need to save document twice. Optional argument “” to AppendTable is optional, you can leave out. Not sure what effect is of “” here.