[Solved] Closing OpenOffice by macro

Creating a macro - Writing a Script - Using the API

[Solved] Closing OpenOffice by macro

Postby Marco2014 » Wed Oct 29, 2014 11:58 pm

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
Marco2014
 
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Postby kingfisher » Thu Oct 30, 2014 12:30 am

The code posted here may work.
Apache OpenOffice 4.1.2 (64 bit) on PCLinuxOS
User avatar
kingfisher
Volunteer
 
Posts: 2116
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Postby Marco2014 » Thu Oct 30, 2014 12:53 am

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   Expand viewCollapse view
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
Marco2014
 
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Postby kingfisher » Thu Oct 30, 2014 1:52 am

That's my lot, sorry. The code still works for me.
Apache OpenOffice 4.1.2 (64 bit) on PCLinuxOS
User avatar
kingfisher
Volunteer
 
Posts: 2116
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Postby kingfisher » Thu Oct 30, 2014 7:12 am

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.2 (64 bit) on PCLinuxOS
User avatar
kingfisher
Volunteer
 
Posts: 2116
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Postby gkissinger » Thu Oct 30, 2014 10:32 pm

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

Code: Select all   Expand viewCollapse view
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
gkissinger
 
Posts: 15
Joined: Thu Apr 25, 2013 1:08 am
Location: Arizona, USA

Re: Closing OpenOffice by macro

Postby Marco2014 » Fri Oct 31, 2014 12:58 am

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
Marco2014
 
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Postby RPG » Fri Oct 31, 2014 2:44 am

LibreOffice 6.1.3.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2152
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Closing OpenOffice by macro

Postby kingfisher » Fri Oct 31, 2014 4:35 am

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.2 (64 bit) on PCLinuxOS
User avatar
kingfisher
Volunteer
 
Posts: 2116
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Postby RPG » Fri Oct 31, 2014 10:45 am

Hello

https://forum.openoffice.org/en/forum/v ... =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 6.1.3.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2152
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Closing OpenOffice by macro

Postby RoryOF » Fri Oct 31, 2014 11:13 am

 Edit: My posting in error to this thread moved: irrelevant to this topic - RoryOF 
Apache OpenOffice 4.1.6 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 27941
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Closing OpenOffice by macro

Postby Marco2014 » Fri Oct 31, 2014 4:44 pm

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   Expand viewCollapse view
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 75 times
Apache OpenOffice 4.1.0 - Windows 7
Marco2014
 
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: Closing OpenOffice by macro

Postby Villeroy » Fri Oct 31, 2014 5:56 pm

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26141
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Closing OpenOffice by macro

Postby RPG » Fri Oct 31, 2014 9:03 pm

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 6.1.3.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2152
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Closing OpenOffice by macro

Postby kingfisher » Fri Oct 31, 2014 11:24 pm

FWIW, this is the wiki developers' page : Closing Documents.
Apache OpenOffice 4.1.2 (64 bit) on PCLinuxOS
User avatar
kingfisher
Volunteer
 
Posts: 2116
Joined: Tue Nov 20, 2007 10:53 am

Re: Closing OpenOffice by macro

Postby Marco2014 » Sat Nov 01, 2014 12:41 am

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 viewCollapse 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

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 83 times
Apache OpenOffice 4.1.0 - Windows 7
Marco2014
 
Posts: 12
Joined: Sun Jul 20, 2014 2:54 pm

Re: [Solved] Closing OpenOffice by macro

Postby Greengiant224 » Sun Nov 02, 2014 2:44 am

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

Linux 2.6.38, KDE 4.9.2, LibreOffice 3.5.* (Java 1.6.0_35) utilising HSQL 1.8.10 & 2.3.*, MySQL, PostgreSQL, SQLite
+ Blood, Sweat and Tears (Application, Determination and Perseverance)
User avatar
Greengiant224
 
Posts: 276
Joined: Wed Jun 09, 2010 3:50 pm
Location: All Over The World


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 6 guests