[Solved] Closing OpenOffice by macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Marco2014
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

[Solved] Closing OpenOffice by macro

Post by Marco2014 »

Hi,
May I close OpenOffice 3.1 (or LibreOffice 4.1) using a macro assigned to a button on a form?
I read several threads but till now my tries failed and soffice.bin didn't stopped.
Thank you in advance
Marco
Last edited by Hagar Delest on Sat Nov 01, 2014 11:47 am, edited 1 time in total.
Reason: tagged [Solved].
Apache OpenOffice 4.1.0 - Windows 7
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Post by kingfisher »

The code posted here may work.
Apache OpenOffice 4.1.9 on Linux
Marco2014
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Post by Marco2014 »

Thank you kingfisher,
I tried it but executing oDoc.close(True), I received the error message "Exception: com.sun.star.util.CloseVetoException".
So I tried first to close the main form "Menu" using ThisDatabaseDocument.FormDocuments.getByName("Menu").close
Here is the code:

Code: Select all

Sub CloseDatabase2
  Dim oEnum 
  Dim oDoc 
  ThisDatabaseDocument.FormDocuments.getByName("Menu").close
  oEnum = StarDesktop.getComponents().createEnumeration() 
Do While oEnum.hasMoreElements() 
   oDoc = oEnum.nextElement() 
   If HasUnoInterfaces(oDoc, "com.sun.star.util.XCloseable") Then 
   oDoc.setModified(False) 
   oDoc.close(True) 
  Else 
   oDoc.dispose() 
   End If 
Loop 
StarDesktop.Terminate() 
End Sub
but I read another error message "variable not defined" and soffice.exe and soffice.bin still hang up.
Apache OpenOffice 4.1.0 - Windows 7
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Post by kingfisher »

That's my lot, sorry. The code still works for me.
Apache OpenOffice 4.1.9 on Linux
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Post by kingfisher »

It may help to add the following line before oDoc.close(True) : oDoc.dispose()

EDIT : the code from the other thread closes all files even those which have been modified and not subsequently saved.
Apache OpenOffice 4.1.9 on Linux
gkissinger
Posts: 19
Joined: Thu Apr 25, 2013 1:08 am
Location: Arizona, USA

Re: Closing OpenOffice by macro

Post by gkissinger »

This is what I use at the end of my backup macro to close out:

Code: Select all

Dim Docs As Object,Components as Object
Components = StarDesktop.getComponents()
Docs = Components.createEnumeration()
		Do While Docs.hasMoreElements()
			Doc = Docs.nextElement()
			Doc.dispose()
		loop	
Exit Sub
Win 7 - OpenOffice 4.1.1
Marco2014
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Post by Marco2014 »

Hi kingfisher,
I added oDoc.dispose() before oDoc.close(True) but I received this message:"Type: com.sun.star.lang.DisposedException Message: Component is already disposed".
Thank you for your efforts.

To gkissinger,
thanks for your help; even if I added Dim Doc As object, soffice.bin doesn't terminate.
Apache OpenOffice 4.1.0 - Windows 7
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Closing OpenOffice by macro

Post by RPG »

LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Post by kingfisher »

One problem is that the close command is returning the 'Exception: com.sun.star.util.CloseVetoException'.

The API page says only :
this exception can be thrown to prevent the environment of any object from closing
Apache OpenOffice 4.1.9 on Linux
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Closing OpenOffice by macro

Post by RPG »

Hello

viewtopic.php?f=20&t=49434&hilit=terminate

I think in short you can read in the link: You can not close/ terminate Openoffice when:
a) The macro is stored in your document
b) BASIC is running
c) There is an open Dialog then BASIC is running
d) When the IDE is open.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Closing OpenOffice by macro

Post by RoryOF »

 Edit: My posting in error to this thread moved: irrelevant to this topic - RoryOF 
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Marco2014
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Post by Marco2014 »

Thank you RPG for your clear framework:
While the other 3 conditions (Basic running, open Dialog, open IDE) haven't been present, the macro "CloseDatabase2" was really stored in a document of mine.
So I transferred it in My Macros> Standard>Module1.
Then I run from the button the following code stored in My Macros:

Code: Select all

sub CloseDatabase2
  Dim oEnum 
  Dim oDoc
  oEnum = StarDesktop.getComponents().createEnumeration() 
Do While oEnum.hasMoreElements() 
   oDoc = oEnum.nextElement() 
   If HasUnoInterfaces(oDoc, "com.sun.star.util.XCloseable") Then 
   oDoc.setModified(False) 
   oDoc.close(True) 
  Else 
  oDoc.dispose() 
   End If 
Loop 
StarDesktop.Terminate() 
End sub
But it stops at oDoc.close(True) and I still receive this; "Type: com.sun.star.util.CloseVetoException"
I'm wondering if there is something I could check in my copy of Open Office.

Furthermore the main directory where Open office is located is owned by me who started the office processes.
You can see, attached, the database I'm testing.
Thank you
Attachments
Database.odb
(11.63 KiB) Downloaded 284 times
Apache OpenOffice 4.1.0 - Windows 7
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Closing OpenOffice by macro

Post by Villeroy »

You can write a program starter in some "real" programming language that starts up the office suite in listening mode, connects to the listening office, plays games with the same UNO-Api as macros do and finally takes care of proper shutdown.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Closing OpenOffice by macro

Post by RPG »

Hello

I have no solution. Once I have tried it and I did not find a solution. I think when you need to terminate OOo then most easy is: Control-Q or closing all documents by hand. The idea of Villeroy can also be a good idea but is outside my knowledge.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Post by kingfisher »

FWIW, this is the wiki developers' page : Closing Documents.
Apache OpenOffice 4.1.9 on Linux
Marco2014
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Post by Marco2014 »

thanks to everybody.
I'm sure I'm not able to manage Villeroy's suggestion.
Anyway, I found out here: http://www.oooforum.org/forum/viewtopic ... b&start=15
a solution that works for the attached database.

Here is the code:

Code: Select all

sub  CloseDatabase2 (oEv as object) 
' macro assigned to a button on a form to close openoffice 
Dim oSvc as object 
msgbox "      Closing OpenOffice" & chr$(10) & "           Goodbye......", 0, "  --WARNING--  " 
oSvc = createUnoService("com.sun.star.system.SystemShellExecute") 
ThisDatabaseDocument.close(true) ' >> close the internal file first 
' pass the required parameters to execute a clean shutdown of soffice.exe 
wait 2000
oSvc.execute("c:\windows\system32\taskkill.exe", " /F /IM soffice.exe /T" , 0) ' >> windows vista/7 syntax 
End Sub
I'm not able to assess how good the code is and if I could use it in a much more complex database (maybe I couldn't).
Marco
Attachments
Database2.odb
(11.97 KiB) Downloaded 283 times
Apache OpenOffice 4.1.0 - Windows 7
User avatar
Greengiant224
Posts: 283
Joined: Wed Jun 09, 2010 3:50 pm
Location: All Over The World

Re: [Solved] Closing OpenOffice by macro

Post by Greengiant224 »

marco:
thanks to everybody.
I'm sure I'm not able to manage Villeroy's suggestion.
Anyway, I found out here: http://www.oooforum.org/forum/viewtopic ... b&start=15
a solution that works for the attached database.

Here is the code:

Code: Select all Expand view
sub CloseDatabase2 (oEv as object)
' macro assigned to a button on a form to close openoffice
Dim oSvc as object
msgbox " Closing OpenOffice" & chr$(10) & " Goodbye......", 0, " --WARNING-- "
oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
ThisDatabaseDocument.close(true) ' >> close the internal file first
' pass the required parameters to execute a clean shutdown of soffice.exe
wait 2000
oSvc.execute("c:\windows\system32\taskkill.exe", " /F /IM soffice.exe /T" , 0) ' >> windows vista/7 syntax
End Sub
As you will see from the macro you need to close the db file before you shutdown the AOO program.
Failure to do this would result in data corruption especially, if you have been inserting/updating data.
I have been using this macro with a button on some of my db's without any detriment for three years.

You can store the macro in the db document or in the standard macro library, the choice is entirely yours.

Glad you got the code to work on your database. :bravo:

Greengiant224

Win 7, Portable AOO 4.1.14, LibreOffice 6.2, 7.4.5 & 7.6.1 (Java 1.7.81 & 1.8.0_281) utilising HSQL 1.8.10 & 2.4.*, MySQL, PostgreSQL, SQLite
+ Blood, Sweat and Tears (Application, Determination and Perseverance)
Post Reply