Page 1 of 2

[Solved] Close OOo from a macro

Posted: Sun Mar 25, 2012 11:17 am
by mike phillips
It would be useful to be able to close the whole shebang from a command button, as in VBA 'application.quit' which closes all forms and the main programme returning the user to a pure Windows environment. I cannot find any reference to an equivalent in OO?

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 11:55 am
by RPG
Hello

I think: stardesktop.terminate
http://www.openoffice.org/api/docs/comm ... sktop.html

Maybe store the macro in myMacros.

See here to the end of the thread.
http://www.oooforum.org/forum/viewtopic ... b&start=15

Romke

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 12:15 pm
by kingfisher
This may be of interest also: Automating OpenOffice on Windows and session manager.

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 12:40 pm
by mike phillips
Having closed all active forms bar the dialog with the 'CLOSE button on it, the 'cose' sub is in mymacros/standard/module1, but 'stardesktop.terminate' drops me into that module IDE and informs me that I cannot 'close' while Basic is running.

KF - I had not thought of running via a script.

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 1:30 pm
by RPG
Hello

We cannot see your program but I think you cannot close OOo from a dialog.
Solution close first the dialog and then close OOo
I have not test it.

Romke

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 2:47 pm
by mike phillips
Solution close first the dialog and then close OOo
There's the problem. 'Basic' still runs.This is the latest incarnation.

Code: Select all

sub main
DialogLibraries.loadLibrary("Standard")
d = CreateUnoDialog(DialogLibraries.Standard.Dialog1) 
d.execute()

sub CommandButton1 (Dialog1 as Object)
d.endexecute()
closeprog()
end sub

sub closeprog()
   stardesktop.terminate()
End sub

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 3:06 pm
by RPG
Hello

I think your code is to much simplified
Try to use one sub less. Why do you use a sub for one line of code.

Romke

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 3:17 pm
by RPG
Hello

I think you must close the sub in an other way.

Make a button. Prepare the button well, this means. The button must close the dialog. Use the properties
  • Default button :Yes
    State
    Button type
When you set the buttons properties well then you can close the dialog. The dialog.execute can give back a return value of 0 or 1.
I think after this you can close OOo. But I have not test it.
I have no example code.

Romke

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 3:43 pm
by RPG
Hello

I did now test some code. I did see when you want terminate OOo then the IDE must be closed. The next code is working.

s

Code: Select all

ub terminateOOo
	oSearchDialog = CreateUnoDialog( DialogLibraries.Hulp.Dialog1 ) ' Dit maak een verbinding naar de dialoog
	oSearchDialog.Execute() '  en dit maakt het zicht baar
	thiscomponent.store
	thiscomponent.close(true)
	stardesktop.terminate
end sub
I don't know a solution for the error I get when I restart OOo. I think we must close first the IDE before terminating.

Romke

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 6:13 pm
by mike phillips
Hulp. ?? Is that a typo?

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 6:15 pm
by RoryOF
It is Dutch for Help.

Re: Close OO from a macro

Posted: Sun Mar 25, 2012 6:26 pm
by mike phillips
Yes, I know, I used to 'mangle' Dutch, but 'Help' gives me method or property not found:Help which is why I asked.

Re: Close OOo from a macro

Posted: Sun Mar 25, 2012 10:04 pm
by RPG
Hello

What name I use there is not important only the three lines lower are important. I did test more and I think it is not a good idea to close OOo complete with macros. On this moment the link I did give and what Kingfischer told can help you.

Romke

Re: Close OOo from a macro

Posted: Sun Mar 25, 2012 11:12 pm
by rudolfo
Ignore my signature and use a macro language that resides outside of OpenOffice. Together with the udk link about automation that kingfisher mentioned the following VBScript code by Bernard Marcelly should do the trick:

Code: Select all

Set StarDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
StarDesktop.terminate

Re: Close OOo from a macro

Posted: Sun Mar 25, 2012 11:52 pm
by RPG
Hello

It is not a real problem to close OOo. But the problem I find: I cannot startup again. The reason is it does not complete close OOo. When I kill all processes of OOo then I can startup again. This does me remember to an earlier version then with some version you can close a document and with an other version it was not possible or must do it in an other way.

Romke

Re: Close OOo from a macro

Posted: Mon Mar 26, 2012 12:21 pm
by mike phillips
I am getting some way with a vbscript opener but I am puzzled that it opens in Libre rather than OO. Windows file association is set to OO for ods. The other brain-teaser is that it only opens the sheet and does ask for permission to load the macro which does happen in OO.

It would seem that the OO developers would do well to implement a similar function to VBA 'application.quit' to avoid having to go around the houses to do it.

Re: Close OOo from a macro

Posted: Mon Mar 26, 2012 1:16 pm
by kingfisher
Can't your script specify the application to use? Why not file a request for enhancement on the issues site? There's a tutorial on the tutorials board about filing a request or 'bug' report.

Re: Close OOo from a macro

Posted: Mon Mar 26, 2012 2:46 pm
by mike phillips
Can't your script specify the application to use?
-errrrr no! I took the script direct from the link from your link at post 3. Trying to be clever, I reckoned that uninstalling LO would do the trick. Ha! Silly me. Script now cannot find the class from Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager").

Obviously the uninstall has removed that. I will now re-install OO and see what happens. It certainly is not made easy!

EDIT: Class restored, but it still does not trigger the security warning for macros.

Re: Close OOo from a macro

Posted: Sun Apr 08, 2012 5:49 am
by skidmog
I'd probably use something like the Windows script I use to force users off some applications.
I have a batch file that runs on a schedule so at 2:00 am in the morning users are logged out of our ERP and OpenOffice.
Mostly - only if you are happy that there is nothinng you'll lose, but you can probably close any open processes inside a macro.
I think I'll use this to add a close button on my machine shop Tool Management System.

taskkill /F /IM soffice.bin
taskkill /F /IM soffice.exe

Re: Close OOo from a macro

Posted: Sat Apr 21, 2012 11:07 pm
by RPG
Hello

Maybe this example can help you and other readers for closing OOo.

There is one problem I cannot solve: I cannot close the BASIC ide when this is running. You must do this by hand.

Be carefull when you test this code. Store first your data. I'm not sure if I have found all problems.

Romke

Re: Close OOo from a macro

Posted: Sun Apr 22, 2012 9:05 am
by mike phillips
Appreciated, RPG. I will have a look. It does seem that unlike VBA you cannot just 'quit' everything but, as you say, you need to shut down the IDE 'manually'.

Re: Close OOo from a macro

Posted: Sun Apr 22, 2012 11:10 am
by RPG
Hello

The last time I try to use as much of this list. Yesterday I did found SaveAll and now Quit. So I hope I can use them for making smaller code. But I think there is still a problem for closing the database. When the database is closed it is possible that not all data is stored. The database does have the possibilities to close all the files who belong to the databasedocument but they are not stored. The same can happens for data in the tables in the database. But this is maybe only true when you open the databasedocument and do not have a good connection.

Learning programming in the API is for me also difficult. I do also learn new things. The last time I do learn to use commands from the list I did point to. Using this table means also you are programming on an other higher level. I have the idea there several levels in the way you can programm in the API. It cost me a lot of time before I could see this. It cost me a lot of time use all the possibillities of loadcomponentfromUrl. This was special true when I use it for opening subcomponents in a database. In this case you can use special command for the the subcomponents. loadComponentWithArguments give also possibilities.

In the beginning I was sure I could better avoid the commands which use the arguments. The reason was I could not understand them. Now I do understand them better I have the idea it is good to use them. This is also true for using the commands who belong to the framework. For me it was true: I could only use those things when I better understand how OOo is working and did work in the way OOo expect.

I do tell this for several reason but one reason is: It is for most people difficult to make code for OOo. You can only master OOo when you try to understand it. For me was true when I did learn more about OOo it force me to work in the way OOo expect.

When I do find shorter code I will post. I hope you enjoy it.

Romke

Re: Close OOo from a macro

Posted: Wed Aug 15, 2012 10:22 pm
by ristoi
I dont understand what is real problem here? You can always shut down manually?

If you want to use macro which is not saving anything an not closing Basic IDE -window you may use this:

Code: Select all

sub Lopetus
Rem rutiini, joka lopettaa OpenOffice-istunnon
Rem EI TALLENNA MITÄÄN!
Rem Muokannut: Risto Jääskeläinen

Rem Part from A. Pitoynak

Dim oDesktop As Object, oDocs As Object
  Dim oDoc As Object, oComponents As Object
  Dim i as Integer 'i counts how many windows are open in OOo
  i = 0
  
  oComponents = StarDesktop.getComponents()
  oDocs = oComponents.createEnumeration()
  Do While oDocs.hasMoreElements()
    oDoc = oDocs.nextElement()
    oDoc.dispose()
  Loop

Rem Nauhoitettua osuutta
rem ----------------------------------------------------------------------
rem define variables
'dim document   as object
'dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
'document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
'dispatcher.executeDispatch(document, ".uno:Quit", "", 0, Array())
Wait 1000				'Tässä sen salaisuus?
dispatcher.executeDispatch(StarDesktop, ".uno:Quit", "", 0, Array())


end sub		'Lopetus
This Lopetus-macro seems to work day and night, with Quickstarter or without, in Windows and in Linux Mint. But I dont know if anybody really need it (-;

Re: Close OOo from a macro

Posted: Wed Aug 15, 2012 11:55 pm
by RPG
Hello

The problem was not closing normal documents but closing also an open IDE and I did see also problems with closing databasedocuments with storing the data and modified forms.

Romke

Re: Close OOo from a macro

Posted: Thu Aug 16, 2012 8:41 am
by mike phillips
Indeed, RPG, and thanks for the renewed interest here from risto. It is the closing of the iDE that I seek a la VB and this appears to be the sticking point.

Re: Close OOo from a macro

Posted: Thu Aug 16, 2012 8:59 am
by RoryOF
I have no experience with this topic (or similar), but I mention that Alt F4 closes applications in Windows and Ubuntu.

Re: Close OOo from a macro

Posted: Thu Aug 16, 2012 9:07 am
by ristoi
I dont understand what is real problem here? You can always take a bigger hammer?

Code: Select all

sub Lopetus2
Rem rutiini, joka lopettaa OpenOffice-istunnon
Rem EI TALLENNA MITÄÄN!
Rem Muokannut: Risto Jääskeläinen
Rem Part from A. Pitoynak

Dim oDesktop As Object, oDocs As Object
  Dim oDoc As Object, oComponents As Object
  
  oComponents = StarDesktop.getComponents()
  oDocs = oComponents.createEnumeration()
  Do While oDocs.hasMoreElements()
    oDoc = oDocs.nextElement()
    oDoc.dispose()
  Loop
Wait 1000					'Ei voi olla ainakaan < 500 !
Rem Raaka peli, suljetaan prosessi käyttöjärjestelmän puolelta:
Shell("c:\windows\system32\taskkill.exe",2,"/F /iM soffice.bin")
end sub		'Lopetus2
Thats closing with IDE open or closed what ever. Nothing is saved.

Re: Close OOo from a macro

Posted: Thu Aug 16, 2012 9:20 am
by RPG
Hello ristoi

When you did read the complete thread and also the links in the beginning you can knew it was done.

Romke

Re: Close OOo from a macro

Posted: Fri Sep 07, 2012 4:38 pm
by pitonyak
ristol,

Although you can dispose a document, it is probably safer to check the returned component to see if it supports close. if it does support close, you can use the macro to set the document to be "not modified", then simply close the document. Probably safer than dispose. Sadly I can't say why it might be safer, but it was the method recommended to me some years back. Perhaps it just feels "kinder" to not use dispose with no real benefit. :knock:

Re: Close OOo from a macro

Posted: Mon Mar 03, 2014 9:03 pm
by mike phillips
Need to revisit this thread: whatever I try I get a 'Recovery' process afterwards. I am currently using ristoi's 'shell' solution to close the main programme since all other methods do the same, but I always seem to be left with a 'cannot close while Basic is running' warning requiring Task Manager and also bizarrely an 'Untitled1 writer' opens on re-running (The programme runs in Calc) . Ristoi's 'hammer' is obviously effective but I would prefer to close it via OO. I just need to close down the main without triggering a 'recovery' process. Any ideas? All dialogs are disposed, files are saved and closed. What else do I need to do?