Solved ... the problem was a missing parameter, "FileNamePrefix" ...
The complete working (for me) code for a query based MailMerge with file output is:
Code: Select all
Sub myMailMerge(sourceDocName as String, dbName as String, queryName as String, outputDocDir as String, outputDocPrefix as String)
Dim oMailMerge as Object
oMailMerge = CreateUnoService("com.sun.star.text.MailMerge")
oMailMerge.DocumentURL = sourceDocName
oMailMerge.DataSourceName = dbName
oMailMerge.CommandType = 1
oMailMerge.Command = queryName
oMailMerge.OutputType = com.sun.star.text.MailMergeType.FILE
oMailMerge.OutputUrl = outputDocDir
oMailMerge.FileNamePrefix = outputDocPrefix
oMailMerge.SaveAsSingleFile=True
oMailMerge.execute(Array())
oMailMerge.dispose()
End Sub
sourceDocName: it's the URL of the document with mailmerge fields
dbName: it's the name of the registered database containing the (see next line)
queryName: the query saved in the database
outputDocDir: directory where I want to put the output
outputDocPrefix: prefix of the filename generated
( It is heavily based on the code I've found here:
http://user.services.openoffice.org/en/ ... 20&t=22804 )
This function can be written better, and there are a lot of other options for the mailmerge. But it works for me, and I'm not a programmer. I hope that someone more
skilled can write a better function for the community
I think that changing CommandType to 0 and Command to the name of the table (for a table based mailmerge) or CommandType to 2 and Command to a SQL query for a direct
query Mailmerge should work, but I have no time to test it now.
Thank to Villeroy for having answered me
Marco