Ask Your Question

Can Librewriter do a headless mailmerge

asked 2016-01-27 07:31:00 +0100

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

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.

edit retag flag offensive close merge delete


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

kunda gravatar imagekunda ( 2016-03-17 17:38:38 +0100 )edit

2 Answers

Sort by » oldest newest most voted

answered 2016-03-29 03:32:16 +0100

Vie1beeg gravatar image

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("")
        'Set MailMerge properties
    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 objects.
        'We can also feed an empty array. In that case the default values above are used.

End Sub
edit flag offensive delete link more


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.

TechColab gravatar imageTechColab ( 2016-12-11 14:05:37 +0100 )edit

answered 2016-03-17 21:02:25 +0100

vmiklos gravatar image

A macro in the document is actually an option. The 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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools



Asked: 2016-01-27 07:31:00 +0100

Seen: 1,176 times

Last updated: Mar 29 '16