Page 2 of 2

Re: Close OOo from a macro

Posted: Thu Jun 05, 2014 8:40 am
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!

Re: Close OOo from a macro

Posted: Thu Jun 05, 2014 10:36 pm
by mike phillips
HHH - much appreciate the pointer to Autolt3 - had not come across that before. That looks to do the job. Thank you.

Re: Close OOo from a macro

Posted: Fri Jun 06, 2014 2:33 am
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.

Re: Close OOo from a macro

Posted: Fri Jun 06, 2014 8:45 am
by mike phillips
Noted

Re: Close OOo from a macro

Posted: Fri Jun 06, 2014 10:13 pm
by mike phillips
HHH - just a little nervous here - I am getting AV activation due to Win32: Evo-gen presence. Any comments?

Re: Close OOo from a macro

Posted: Fri Jun 06, 2014 10:18 pm
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.

Re: Close OOo from a macro

Posted: Fri Jun 06, 2014 10:27 pm
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

Re: Close OOo from a macro

Posted: Sat Jun 07, 2014 11:06 pm
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")

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 4:09 am
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/

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 9:27 am
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!!

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 9:30 am
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.

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 11:27 am
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.

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 11:37 am
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.

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 11:38 am
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

Re: Close OOo from a macro

Posted: Sun Jun 08, 2014 11:52 am
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. :(

Re: Close OOo from a macro

Posted: Thu Jun 12, 2014 11:05 pm
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?

Re: Close OOo from a macro

Posted: Fri Jun 13, 2014 2:26 am
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)

Re: Close OOo from a macro

Posted: Fri Jun 13, 2014 8:46 am
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'....

Re: Close OOo from a macro

Posted: Fri Jun 13, 2014 11:32 am
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.

Re: Close OOo from a macro

Posted: Fri Jun 13, 2014 12:11 pm
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)).

Re: Close OOo from a macro

Posted: Fri Jun 13, 2014 12:52 pm
by mike phillips
Hmm - still left with the basic window open. I have no idea how to hard code that in Autolt!

Re: Close OOo from a macro

Posted: Sun Jun 15, 2014 10:54 am
by HHHz
Try shortening the 3rd line to:

Code: Select all

   vShell.execute("C:\AutoIt3\CloseOODoc.exe", """My Macros & Dialogs"" 2000", 1)

Re: Close OOo from a macro

Posted: Sun Jun 15, 2014 11:11 am
by mike phillips
BINGO! Many thanks. That works a treat.

Re: [Solved] Close OOo from a macro

Posted: Fri Jul 11, 2014 10:41 am
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

Re: [Solved] Close OOo from a macro

Posted: Fri Nov 10, 2017 6:15 pm
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