After quite a bit of Googling and forum reading, I have an OOBasic macro that opens a document that is set up for mail merge, executes the merge and closes Writer. This is working except that OpenOffice believes it has crashed. I believe this has something to do with the terminate() call.
Does anyone know how to close OpenOffice properly once I'm done? I'm on Windows XP at the moment, but I need a solution that is platform independent.
Here's my code:
Code: Select all
Sub MergeIt
'Get current database settings
oDoc = ThisComponent
oDocSettings = oDoc.createInstance("com.sun.star.text.DocumentSettings")
oDBSourceName = oDocSettings.CurrentDatabaseDataSource
'Create a new database context and connection from the existing file
oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDBSource = oDBContext.GetByName(oDBSourceName)
oConnection = oDBSource.GetConnection("","")
'Now set up a new MailMerge using the settings extracted from that doc
oMailMerge = CreateUnoService("com.sun.star.text.MailMerge")
oMailMerge.DocumentURL = "file:///d:/jobs/beamer/ootest.odt"
oMailMerge.DataSourceName = "Merge1"
oMailMerge.CommandType = 0
oMailMerge.Command = "ootest"
oMailMerge.OutputType = 1
oMailMerge.execute(Array())
oConnection.dispose() 'so that you don't get locked out of source file
oDoc.close(true)
Stardesktop.terminate()
End Sub