Can Librewriter do a headless mailmerge

I have Librewriter generating invoices from through a mail-merge. The data comes from a spreadsheet.

Trying to figure out how to do the mail-merge automatically. I have a spreadsheet in Calc that is printed out every morning via a cron job no problem. However when I call “soffice --headless --print-to-file test.odt” I only get the printed template without fields rather than the completed mail merge.

Is there any way to call the mail merge from the command line, or from a macro in the odt file?

Any help or suggestions would be greatly appreciated.

can you share your workflow of generating invoices ? I’d like very much to do the same

A macro in the document is actually an option. The com.sun.star.text.MailMerge service, together with the basic createUnoService() function probably allows you to automatically perform a mail merge from a macro. The sw/qa/extras/mailmerge/mailmerge.cxx testcase in the source code uses this method to automatically test mail merge.

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

Hi, newbie to LO automation. Could you tell me where the above example would be in?
The comment below suggests that it’s not a macro within the document (but could be).
I.e. In what file would I put that & what command line would I use to execute it? Thanks.