Using regular expressions
To find strings in a certain context, you should use regular expressions. We will not
explain them in detail here since the StarOffice documentation contains all necessary
information. Suppose that in a bibliography each entry starts with the title followed
by the author. You cannot search for only the lines in the document containing these
entries with a simple string, since there is no common word for all entries. However,
they share a common structure- each line starts with a number contained in brackets.
Using a regular expression, you can search for this structure. The following program
fragment exchanges title and author:
Dim oSearchDesc As Object, oCursor As Object
Dim oFoundAll As Object, oFound As Object
Dim nLen As Integer, n As Integer
Dim nEndAuthor As Integer
Dim nStartTitle As Integer, nEndTitle As Integer
Dim sAuthor As String, sTitle As String
Dim s As String, sRest As String
oSearchDesc = oDocument.createSearchDescriptor()
oSearchDesc.SearchString = "^[[0-9]+]"
oSearchDesc.SearchRegularExpression = TRUE
oFoundAll = oDocument.findAll( oSearchDesc )
For n = 0 To oFoundAll.Count - 1
oFound = oFoundAll(n)
REM Or: oFound=oFoundAll.getByIndex(n)
oCursor = oText.createTextCursorByRange(oFound)
Using StarOffice API - Building blocks 57
s = oCursor.String
nEndAuthor = InStr(1, s, ";") -1
nLen = nEndAuthor
sAuthor = Left(s, nLen)
nStartTitle = nEndAuthor + 2
nEndTitle = InStr(nStartTitle, s, ";") -1
nLen = nEndTitle - nStartTitle + 1
sTitle = Mid(s, nStartTitle, nLen)
nLen = Len(s) - nEndTitle
sRest = Right(s, nLen)
oCursor.String = sTitle + " " + sAuthor + sRest
After creating the search descriptor, its SearchString is set to ^[[0-9]+]. In
plain words this means: "Find all lines beginning with a left bracket. This bracket
must be followed by at least one digit and a closing bracket.". The caret (^) stands
for "beginning of the line", "[0-9]" denotes all digits, and the following plus sign
means "at least one". We’ll not go into more detail here, the StarOffice API Reference
Manual contains more information on regular expressions. The search descriptor’s
searchRegularExpression attribute must be set to TRUE to let it know that it
should not look for the literal string "^[[0-9]+]".
All lines matching the regular expression are then found with findAll(). It returns
an XTextRange() interface which is passed to createTextCursorByRange().
The new cursor is thus automatically positioned at the matching regular expression.
By moving it to the next word and then to the end of the paragraph, we span a text
range that contains the whole bibliography entry but the leading number. This string
is finally modified with some lines of pure StarBasic. They simply find the semicolons
separating the author and the title and re-assemble the string from these parts.
4.3.7 Inserting tables, frames, etc.
As mentioned in Section 4.3.1 “The structure of text documents” on page 47, a text
document can contain tables and other elements besides paragraphs of text. We’ll
show you how to insert tables and frames in your text document in this section.
A table is a special object that you can insert into a text document. You can think of
it as a simple spreadsheet - a very simple one, in fact. First of all, you will see how to
insert a simple table:
Dim oTable As Object
oCursor = oText.createTextCursor()
oTable = oDocument.createInstance("com.sun.star.text.TextTable")
oText.insertTextContent(oCursor, oTable, FALSE)
The table is created by calling createInstance() and then initialized to contain
five rows and nine columns. It is then placed in the current document with a call to
insertTextContent(). This methods expects a textCursor as its first element.
In our example, this cursor is simply placed at the beginning of the document.
Your document now contains an empty table. Since you would probably want to
place text or other things inside this table, we’ll show you how to do this next:
Dim oTableCursor As Object
Dim sCellName As String
Dim oCell As Object
oTableCursor = oTable.createCursorByCellName(oTable.CellNames(0))
Dim mTableHeaders(8) As String
mTableHeaders(0) = "Field"
mTableHeaders(1) = "Format"
mTableHeaders(2) = "AutoIncrement"
mTableHeaders(3) = "Descending"
mTableHeaders(4) = "PartOfPrimaryKey"
mTableHeaders(5) = "Required"
mTableHeaders(6) = "Scale"
mTableHeaders(7) = "Size"
mTableHeaders(8) = "Type"
For n = 0 To 8
sCellName = oTableCursor.getRangeName()
oCell = oTable.getCellByName(sCellName)
To move around in a table, you have to create a tableCursor first. The sample
code above uses the method createCursorByCellName() for this and then
moves the cursor to the top left cell of the table. After initializing the array
tableHeaders with the column headers of the table, it moves the cursor to each of
the cells in the first row with goRight(). The content of the cell is then set by
assigning to its String property.
If you want to insert numbers into a table, you’ll probably not want them to be
displayed left justified, which is the default for all cells. To change the justification
for a cell depending on its content, you can use this code snippet:
If IsNumeric(s) Then
oCellCursor = oCell.createTextCursor()
oCellCursor.paraAdjust = com.sun.star.style.ParagraphAdjust.RIGHT
We are using a text cursor here to hard format a cell if it contains a number.
Alternatively, you might define a special style for numbers and assign it to the cell: