[Solved] Close OOo from a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

Mike,
I was also having similar issue. After going through and trying all the methods described here, I finally decided to outsource the fix by calling another program to close OO.

Assuming you are in Windows environment... I created an AutoIt3 program to look for Window title of the document and call "WinClose" on that window. See attached file for code (you'll need to first rename the extension to .au3, and then use AutoIt3 to compile it)

Then in your macro, do something like this...

Code: Select all

	
Dim vShell as Variant
	vShell = createUnoService("com.sun.star.system.SystemShellExecute")
	vShell.execute("C:\AutoIt3\CloseOODoc.exe", "Learn-OO-program.odt", 0)

The execute line's first parameter is the path to the compiled script. 2nd parameter is the title of the file to close.
 Edit: Uploaded version 1.1 - added an optional parameter to specify number of ms to pause before execution. Default is 1000ms (1s). This maybe important because it's not possible to close OpenOffice while BASIC is still running. This waits for BASIC to exit before closing OO. 
Hope this helps!
Attachments
CloseOODoc.txt
Change file name extension to .au3, version 1.1, added a 2nd parameter (optional) to specify number of ms to pause before execution.
(1.99 KiB) Downloaded 432 times
Last edited by HHHz on Fri Jun 06, 2014 2:33 am, edited 1 time in total.
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

HHH - much appreciate the pointer to Autolt3 - had not come across that before. That looks to do the job. Thank you.
OpenOffice 4 on Windows 7
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

Hi Mike, I found a possible problem with the script and made a modification to the original post. Please use the new version.
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Noted
OpenOffice 4 on Windows 7
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

HHH - just a little nervous here - I am getting AV activation due to Win32: Evo-gen presence. Any comments?
OpenOffice 4 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Close OOo from a macro

Post by RoryOF »

Run an indepth anti virus scan, with latest definitions. Also a malware scan. The text file should not (cannot?) carry any virus; your system may be compromised from some other source.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Rory - not the text file, the compilation triggered the quarantine. I also had a similar event downloading the editor. I will submit the stuff to the AV folk to see if it is a false pos.

All relevant folders coming up clean on two different scans
Last edited by mike phillips on Sun Jun 08, 2014 9:30 am, edited 1 time in total.
OpenOffice 4 on Windows 7
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

HHH - have you had a similar issue? I have submitted the exe to my AV company for analysis waiting for any response. I seem to be missing a few lines from the end of Close00Doc.txt? Last visible line is
Else
;_HH_DbgMsg("Window not found")
OpenOffice 4 on Windows 7
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

There should be an "Endif" as the last line. Mine has it, not sure how it's lost.

As for AV issue... It happens every now and then with AutoIt. I run Windows 7's Windows Microsoft Security Essential with no problem.
Here is the thread on how to deal with it: http://www.autoitscript.com/forum/topic ... -infected/
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Thanks HHH - missing if opened in Notepad but there in Wordpad????

Not sure if my send to my AV was successful or not (no reply) but I appear to be able to run Autolt this am without the warning. Thanks for the help/patience!!
OpenOffice 4 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Close OOo from a macro

Post by RoryOF »

mike phillips wrote:Thanks HHH - missing if opened in Notepad but there in Wordpad????
It may be to do with whether that last line is terminated with a newline character or not.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

I thought of another possibility to achieve closing OO document without using external program, though it doesn't exit OO completely. Depending on exactly what you want, this may not meet your requirement...

The solution is:

1) put your Macro inside a dummy OO document. This is your "driver" document.

2) In your macro, use "StarDesktop.LoadComponentFromUrl" to open the document you want to operate on. And run on it. You get a document object reference back for example: oMyDoc = Stardesktop...

3) You can close the document then using oMyDoc.close(true).

Again, this would keep your driver document open, but at least your operating document isn't opened at the end.
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Yes - tried similar, HHH, but the 'desire' is to enable the less 'able' user to clear the screen of any OO windows through a button click (as in VBA) and it looks as if your Autolt will do that. Given time I will be 'learning' and trying.
OpenOffice 4 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Close OOo from a macro

Post by RoryOF »

I haven't read the detail of this thread, but a quick search threw up this, which I quote in full as the original site is unavailable

Posted: Sun Jul 24, 2011 4:57 am Post subject: save and close document macro

very simple macro that saves current OOo document and close it.

I assigned it to the "Esc" key


Code:
sub SaveAndClose
dim document as object
dim dispatcher as object
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:Save", "", 0, Array())
ThisComponent.close(True)
end sub

I found it useful as an "auto-save" workaround when closing a document without manually selecting the "Yes" or "No" saving dialog window
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Thanks, Rory - I'll give that a go when I have more time. The problem all the way through is that unlike VBA, any attempt to close OO leaves a basic IDE window running. I hope autolt will surmount this issue. Further development of my 'suite' has had to sit on the back burner lately. :(
OpenOffice 4 on Windows 7
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Autolt doing what it says on the tin, but I am closing down to a window called 'My Macros & Dialogs.Standard.Module1'. How can I just close to a 'clean' windows screen?
OpenOffice 4 on Windows 7
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

So you have 2 windows you need to close? If you run your macro without the Macro IDE open, you shouldn't have that 'My Macros & Dialogs.Standard.Module1' open. In any case, if you just wanted to be sure, then it sounds like you need 2 different shell execute statements:

Code: Select all

Dim vShell as Variant
   vShell = createUnoService("com.sun.star.system.SystemShellExecute")
   vShell.execute("C:\AutoIt3\CloseOODoc.exe", "My Macros & Dialogs.Standard.Module1 2000", 1)
   vShell.execute("C:\AutoIt3\CloseOODoc.exe", "My-Doc.odt 1000", 1)
This would make 2 calls, first one basically schedules the close of My Macros... to 2 seconds later. It's important to schedule this one first because it might not get executed if the document that contains your macro gets closed.
The 2nd one closes the document that contains and runs your Macro (Remember to change My-Doc.odt to the name of your document!)
Lastly, I also changed the last parameter to 1 to ignore any errors from the shellexecute statements (See this for info: http://www.openoffice.org/api/docs/comm ... Flags.html)
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Again, thanks. I had tried 2 calls but obviously not correctly, so will try that. How do I run my macro "without the Macro IDE open"? It just seems to 'be there'....
OpenOffice 4 on Windows 7
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

HHH - unfortunately even using your lines I get the 'Window cannot be closed blah blah Basic... etc' Tried different delays, re-ordering lines with delay etc.
OpenOffice 4 on Windows 7
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

Oh yeah, sorry about that... I missed that there are spaces in the parameter!!! Need to quote the parameter:

Code: Select all

Dim vShell as Variant
   vShell = createUnoService("com.sun.star.system.SystemShellExecute")
   vShell.execute("C:\AutoIt3\CloseOODoc.exe", """My Macros & Dialogs.Standard.Module1"" 2000", 1)
   vShell.execute("C:\AutoIt3\CloseOODoc.exe", "My-Doc.odt 1000", 1)
Another way to fix this is to hard-code the close of "My Macros" window inside AutoIt code...
-----------
You could run the macro from "Tools-->Macro-->Run Macro..."
If you do it this way, RoryRF's method does seem to close the document correctly (I thought I tried it before and it didn't work, but now it always works (except in your case when the Macro IDE is open)).
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

Hmm - still left with the basic window open. I have no idea how to hard code that in Autolt!
OpenOffice 4 on Windows 7
HHHz
Posts: 10
Joined: Thu Jun 05, 2014 7:59 am

Re: Close OOo from a macro

Post by HHHz »

Try shortening the 3rd line to:

Code: Select all

   vShell.execute("C:\AutoIt3\CloseOODoc.exe", """My Macros & Dialogs"" 2000", 1)
OpenOffice 4.1.0 on Win7; loves AutoIt3
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Close OOo from a macro

Post by mike phillips »

BINGO! Many thanks. That works a treat.
OpenOffice 4 on Windows 7
User avatar
antekg
Posts: 14
Joined: Sat Oct 17, 2009 10:03 pm
Location: Poland, Warsaw

Re: [Solved] Close OOo from a macro

Post by antekg »

I started to use in vbs following procedure

Code: Select all

oDoc.Store
oDoc.IsModified
oDoc.Close(True)
It works perfectly so far, however somehow artificial
AOO 4.1.4 (Polish) on Windows 10 (64b) / AOO 4.1.0 on Windows Vista / (LibreOffice on Mageia Linux)
nickGiard
Posts: 16
Joined: Fri Nov 10, 2017 5:57 pm

Re: [Solved] Close OOo from a macro

Post by nickGiard »

Hi
First i wont agree Pitonyak for his fantastic OOME : Thank you very very much !!!!
I have the same problem, are three days i have spend for that.
I think that if a macro is running, the root start of the tree macros is necessary in a macro in a document.
Obliviously when this document is closed via that macro, it breaks the running macro itself and this is error of closing.
Maybe I can start a macro outside a document? I dont know how does it. I think that Pitonyak know it !!!! :) :)
By
LibreOffice 6.3 on Windows 10 64bit
Post Reply