This is what I used in the end.
It takes the result of a query of my libreoffice database file and does a mailmerge automatically. I run it on as a cron job every morning to generate packing slips for my business.
Sub mergeit()
oMailMerge = CreateUnoService("com.sun.star.text.MailMerge")
'Set MailMerge properties
'http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/MailMerge.html
oMailMerge.DocumentURL = "file:///{yourfile}.odt" 'Source document (forward slashes only)
oMailMerge.DataSourceName = "{your database}" 'name of data source (included in source document)
oMailMerge.CommandType = 1 '0 = table name, 1 = query name, 3 = SQL command
oMailMerge.Command = "TODAY" 'name of table in data source
oMailMerge.OutputType = 2 '1 = printer, 2 = file, 3 = email
oMailMerge.OutputUrl = "file:///{your output dir}" 'output directory (forward slashes only)
'There are 2 ways to generate output: one single file or multiple individual files
'1. Destination is a single file
oMailMerge.FileNameFromColumn = False 'explicitly set output file name
oMailMerge.FileNamePrefix = "{output}" 'output file name (excl. extension)
oMailMerge.SaveAsSingleFile = True 'save as single file
oMailMerge.SaveFilter = "writer_pdf_Export"
'2. Output to individual files
'oMailMerge.FileNameFromColumn = True 'get file name from data source column
'oMailMerge.FileNamePrefix = "column1" 'name of data source column
'oMailMerge.SaveAsSingleFile = False 'save as multiple files
'Execute MailMerge
'The execute method takes a sequence (array) of com.sun.star.beans.NamedValue objects.
'We can also feed an empty array. In that case the default values above are used.
oMailMerge.execute(Array())
oMailMerge.Dispose()
thiscomponent.store
thiscomponent.close(true)
stardesktop.terminate
End Sub