Placing data in tables can be a time-consuming part of creating a report, but tables make a document easier to read, so it's worth automating the process. The next macro takes a file and loads it into a table. In this case the table is populated with the username and home directory fields from the /etc/passwd file:
Sub doPasswdTable
Dim filenumber As Integer
Dim lineNumber As Integer
Dim lineCount As Integer
Dim iLine As String
Dim file as String
Dim doc as object
Dim table as object
Dim cursor as object
Dim cellname as object
Dim cell as object
file = "/etc/passwd"
'Get line count
lineCount = 0
filenumber = Freefile
Open file For Input As filenumber
While not EOF(filenumber)
Line Input #filenumber, iLine
If iLine <> "" then
lineCount = lineCount + 1
end if
wend
Close #filenumber
doc = thisComponent
cursor=doc.text.createTextCursor()
table=doc.createInstance("com.sun.star.text.TextTable")
table.initialize(lineCount+1,2)
doc.Text.insertTextContent(cursor,table,False)
cell = table.getCellByName("A1")
cell.string="Username"
cell = table.getCellByName("B1")
cell.string="Home Directory"
filenumber = Freefile
lineNumber = 2
Open file For Input As filenumber
While not EOF(filenumber)
Line Input #filenumber, iLine
If iLine <> "" then
Dim iArray
iArray = split(iLine,":")
cell = table.getCellByName("A" & lineNumber)
cell.string=iArray(0)
cell = table.getCellByName("B" & lineNumber)
cell.string=iArray(5)
lineNumber = lineNumber + 1
end if
wend
Close #filenumber
End Sub
Most of the coding is fairly standard -- you can see an example of a while..wend loop, and an if...end if statement. You may also notice the statement doc=thisComponent; this allows me to refer to doc rather than the more cumbersome thisComponent.
Something that may not be as obvious is that Basic is not case-sensitive. Therefore cell = table.getCellByName("A" & lineNumber) will work as well as CELL = table.getcellbyname("A" & LINENUMBER). We use mixed upper- and lower-case letters purely for ease of reading.
The line filenumber = Freefile assigns a unique ID to the filenumber variable, without your having to remember any IDs that you've already assigned.
The split command is particularly useful. It takes an string and returns an array of substrings, broken up according to the delimiter that you supply. In the example above we've used a colon (:) to identify the field separator in the passwd file.
Finally, one line that may look strange at first is Open file For Input As filenumber. This refers to the fact that we're using it as an input to the subroutine, not that we're going to input anything to it.
In conclusion
This is just the briefest of introductions to using Basic in OpenOffice.org Writer. We haven't covered the use of forms, dealing with other documents, or how to obtain information from datatabases.
If you want to learn more about programming in OpenOffice.org Basic, the OpenOffice.org help comes with a list of all the functions and subroutines that are available. You can also try using the OpenOffice.org Record Macro facility, and then examine the code that the application builds for itself.
OpenOffice.org Basic is a useful tool for automating everyday tasks -- just one more way of working smarter instead of harder.
Note: Comments are owned by the poster. We are not responsible for their content.
portability warning
Posted by: Anonymous Coward on October 08, 2005 03:26 AMIt adds another dependency on the original program/set of programs used to edit it.
Ask people who tried to move documents containing WordBasic/VBA to Oo.
#