This post is about reporting data with OpenOffice and Visual Basic.
The documentation of the API in VB was not good, but you can find lot of examples in JAVA and translate to VB. In this page you can find some VB functions can help you to do that
You can start on the development page of OpenOffice (VB/DELPHI) and his forum
Now I will start with some examples: how to open a template file, replace Strings, modify tables and save files.
Open documents
Set oSM = CreateObject("com.sun.star.ServiceManager")
Set oDesktop = oSM.createInstance("com.sun.star.frame.Desktop")
srcFile = ConvertToUrl(FileName)
Set oDoc = oDesktop.loadComponentFromURL(SrcFile, "_blank", 0, args())
ConvertToUrl() function is in this pagethis page
Search and replace strings
Private Sub Search_Replace(oDoc As Object, SString As String, RString As String) Dim objText As Object, objCursor As Object Set objText = oDoc.GetText Set objCursor = objText.createTextCursor Dim oSrch As Object Set oSrch = oDoc.createReplaceDescriptor oSrch.setSearchString (SString) oSrch.setReplaceString (RString) Debug.Print oDoc.replaceAll(oSrch) End Sub
merge with other documents
Set oText = oDoc.GetText Set oCursor = oText.createTextCursor() Call oCursor.gotoEnd(False) Call oCursor.insertDocumentFromUrl(SrcFile2, args())
Work with tables:
Set oTables = oDoc.getTextTables()
Set oTable = oTables.getByName("Table1")
'select table with name Table1
'add row
i = oTable.GetRows().Count - 1
Call oTable.GetRows.insertByIndex(i - 1, 1)
'set value, columns=chr(65+x)
oTable.getCellByName("A" & i).setString StrValue
'del last
Call oTable.GetRows.removeByIndex(oTable.GetRows().Count - 1, 1)
Useful links
Official development page of OpenOffice
Programming OpenOffice.org with Visual Basic
Pages with code
change font style in cell (can be used in OOWriter tables)
Codesnippets for VB
lots of interesting functions