Closing OpenOffice

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Hammer
Posts: 4
Joined: Mon Dec 17, 2007 10:34 pm

Closing OpenOffice

Post by Hammer »

Firstly: I speak a number of programming languages, but I am not yet familiar with the OOo API or object model. I'm trying to feel my way through this.

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
The plural of anecdote is not data.
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Closing OpenOffice

Post by B Marcelly »

Hi,

In your code, these instructions are not used for your mailmerge. If you have no other use you can delete them.

Code: Select all

   'Get current database settings 
   oDoc = ThisComponent
   oDocSettings = oDoc.createInstance("com.sun.star.text.DocumentSettings") 
   oDBSourceName = oDocSettings.CurrentDatabaseDataSource 
   oDBSource = oDBContext.GetByName(oDBSourceName)
   oConnection = oDBSource.GetConnection("","")
   oConnection.dispose()         'so that you don't get locked out of source file 
   oDoc.close(true)
The output of your mailmerge is the printer. I suspect your problem is because the execute method returns before the print job has been queued to the printer. Insert these instructions before oMailMerge.execute

Code: Select all

Dim Props(0) As New com.sun.star.beans.PropertyValue
Props(0).Name = "Wait"
Props(0).Value = true
oMailMerge.PrintOptions = Props()
Hope this helps.
______
Bernard
User avatar
Hammer
Posts: 4
Joined: Mon Dec 17, 2007 10:34 pm

Re: Closing OpenOffice

Post by Hammer »

B Marcelly wrote:In your code, these instructions are not used for your mailmerge. If you have no other use you can delete them.
I think those instructions are what is making sure the data file gets unlocked after I'm done. Without them, the csv file is locked and I can't overwrite it.
The output of your mailmerge is the printer. I suspect your problem is because the execute method returns before the print job has been queued to the printer. Insert these instructions before oMailMerge.execute
Thanks! I'll give this a try.
The plural of anecdote is not data.
User avatar
AndrewZ
Volunteer
Posts: 633
Joined: Mon Oct 08, 2007 1:25 am
Location: Colorado, USA

Re: Closing OpenOffice

Post by AndrewZ »

My OpenOffice.org also crashes on terminate (in PyUNO on Linux), but I just ignore it. It's harmless in my case. :)

http://qa.openoffice.org/issues/show_bug.cgi?id=59026
* Did you solve your problem? Do others a favor: Post the solution
* Visit OpenOffice.org Ninja (blog) and OpenOffice.org Ninja Wiki for news, previews, troubleshooting, etc.
OOo 3.0.X on Fedora 9 + XP
User avatar
Hammer
Posts: 4
Joined: Mon Dec 17, 2007 10:34 pm

Re: Closing OpenOffice

Post by Hammer »

Hi Bernard,

I tried your suggestion, but it's throwing an IllegalArgumentException on PrintOptions. Property type mismatch or property not set. I checked the docs and MailMerge does have a PrintOptions property as of version 2. Wait with true looks valid. Do I need to declare a PrintOptions object or something?

Code: Select all

Sub MergeIt

	'oDoc was opened from a URL, so now get oDoc's 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")

	Dim Props(0) As New com.sun.star.beans.PropertyValue
    Props(0).Name = "Wait"
    Props(0).Value = True
    oMailMerge.PrintOptions = Props()
	
	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
The plural of anecdote is not data.
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Closing OpenOffice

Post by B Marcelly »

Hammer wrote:I tried your suggestion, but it's throwing an IllegalArgumentException on PrintOptions. Property type mismatch or property not set.
Yes, I got the same message using a test merge. Probably PrintOptions does not work. Xray says it is a sequence of sequence of Property Value, this is inconsistent with the IDL description. I tried an array of array, no success.
Anyway, execute is documented as doing the job synchronously, so it should not be necessary to wait. In my test, execute returns after about one second for a small print.

I think the problem is elsewhere. You say you use a csv file, that is more complex than using a real database. Perhaps you have something else to release or disconnect before closing.
Can't help more :cry:
______
Bernard
User avatar
Hammer
Posts: 4
Joined: Mon Dec 17, 2007 10:34 pm

Re: Closing OpenOffice

Post by Hammer »

Rats. Currently, a test merge with two records with two small fields each is taking between 45 seconds and four minutes to spool to the printer and is crashing on the way out. It runs fine if I run it manually through the UI. I've contacted a couple of the people on the consultants listing, as I am wiling to pay for some help, but I haven't heard back from them. I'm pretty tenacious about this kind of thing, and I really want this to work, but I am beginning to despair. I suspect that this is entirely the wrong way to be doing this, but I'm not sure what I should be doing.
The plural of anecdote is not data.
Post Reply