讀取RTF域中的Table

This code is designed to create a non-tabbed table of two rows and five columns in a rich text field to display information. I've added in static text but it could be used to display information from a document as well.

I've implemented this as an agent, as the code this came from was used in this way, but it could be a button, query open etc. You can use the same classes to create a tabbed table, but this tip doesn't do that. This code works with R6 and above only.

  
  Code: Sub Initialize
 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(4) As String
 Dim sHeaders(4) 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(4) As 
NotesRichTextParagraphStyle
 
 Set dbCurr = sess.CurrentDatabase
 Set dcProcess = 
dbCurr.UnprocessedDocuments
 
 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"
  '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"
  
  '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 * 10.73
  
  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,5,"",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 5 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 4
    '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
   Call rtt.AddRow
   Call rtnav.FindNextElement
(RTELEM_TYPE_TABLECELL)
   For iCtr=0 To 4
    Call rtiItem.BeginInsert(rtnav)
    Call rtiItem.AppendText(sData(iCtr))
    Call rtiItem.EndInsert
    Call rtnav.FindNextElement
(RTELEM_TYPE_TABLECELL)
   Next
   
   Call docProcess.Save(True,False)
   Set docProcess = 
dcProcess.GetNextDocument(docProcess)
  Wend
 End If
 
End Sub
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章