Clean closedown of LibreOffice when Form closes

Hi, I wish to

  1. automatically open a Form when LibreOffice is run

  2. for the LibreOffice GUI to disappear but to leave the form running

  3. to clean up LibreOffice when the form is closed

For 1) and 2) I have seen the posts by Ratslinger July 6 2017 (108166) which reference Doug’s answer July 19 2015 to post (53310) by jay Arr.

In Ratslinger’s answer on July 6 2017, he says concerning Sample 2: “IMPORTANT! If you just close the form, the .odb is still active in memory! To Exit properly, use Ctrl & Q keys OR from Instruments menu select File->Exit LibreOffice”.

As the users of my Form will not be proficient in IT or know how to close LibreOffice, is there any way that a macro can be defined to ensure that when the Form is closed, that the cleanup operations recommended by Ratslinger get done automatically?

The usage I expect of this application is that a user will press a button which from his/her point of view will cause my Form to be seen. He/She will not know about LibreOffice and when finished with the Form will somehow close it, by pressing the X icon top right hand of the form. When that happens, I need to ensure that LibreOffice closes down cleanly.

Edit 11 Feb 2020

Here is the Sample 2 macro code used when opening the document, it is 99% the same as that proposed by Ratslinger in his original Sample 2 example from 2017

option explicit

global Continue as Boolean

Sub HideDBWinOpenSwitchboard
' bound to Open Document event

	Dim tc As Object
	Dim i As Integer
	Dim cntrllr As Object
	Dim frm As Object
	Dim appWindow As Object
	Dim comWin As Object
	Dim FrmContainer As Object
	Dim rootFrm As Object
	Dim rootDoc As Object
	Dim ctrllt As Object

'added by Ratslinger
	Continue = true

REM 'catch any error to give alternate instructions to user'
'  	On Local Error Goto ErrHandOpenDB

REM 'if applied to the `Open Document` event `ThisComponent` is equivalent to `ThisDatabaseDocument`'
REM 'the difference being `ThisComponent` works'
  	tc = ThisComponent

REM 'this loop is to work around a delay in defining `ThisComponent` by LO'
  	i = 1
  	Do Until InStr(tc.dbg_properties, "CurrentController" ) > 0 Or i  >= 70
    		Wait 5
    		tc = ThisComponent
    		i = i + 1
  	Loop

REM 'work around more LO limitations when atteming to run on `Open Document` event'
'  	tc.connectController(tc.CurrentController)
  	tc.Title = "connection_asmedb.odb"

REM 'here is the object hierarchy to get to the database window'
  	cntrllr = tc.CurrentController

REM 'belt and suspenders, this might not be necessary'
  	If cntrllr.isConnected = False Then 
    		cntrllr.Connect
  	End If

REM 'more of the object hierarchy'
  	frm = cntrllr.Frame
  	appWindow = frm.ContainerWindow

REM 'make main application window disappear'
  	appWindow.setVisible(False)
  	appWindow.IsMaximized = False

REM 'FYI this is the window inside the application window, if can be resized independently'
REM 'but have not seen any use for this yet'

REM 	comWin = frm.ComponentWindow

REM 'open switchboard form'
  	FrmContainer = tc.FormDocuments.getByName("frm_SearchArchive")
  	FrmContainer.open
  	rootFrm = FrmContainer.Component.getDrawPage.getForms
  	rootDoc = rootFrm.parent
  	ctrllr = rootDoc.CurrentController
  	frm = ctrllr.Frame

REM 'apply title of database set forth above plus title of form'
  	frm.Title = tc.Title & ": frm_SearchArchive"

REM 'maximize the switchboard window'
'  	frmWindow = frm.ContainerWindow
'  	frmWindow.IsMaximized = True

REM 'FYI again here is the window inside the form window'
REM 	frmComp = frm.ComponentWindow

REM 'adjust zoom of switchboard'
'  	ctrllr.ViewSettings.ZoomValue = 90
'  	Exit Sub
'  	ErrHandOpenDB:
'  	MsgBox "The intro did not go as planned.  Browse to `Forms` and open the `Switchboard`."

' added by Ratslinger
	do while Continue
    		wait 20
	loop
	ThisComponent.close(true)

End Sub

Sub CloseBase
' bound to Document Closed event
	Continue = false
End Sub

Hello,

Before going too far, this is confusing:

The usage I expect of this application is that on a particular website

Base is not Internet oriented. Do not comprehend your expectation using Base on a website. Previous questions have dealt with using a connection to a Lubuntu or Windows server and not the Internet. Entirely different situation.

Also, when referencing other questions/answers, please provide an actual link not just a reference.

Thank You.

It would actually be best if you presented a description of what the entire process is - who and how. Disclosing piece by piece causes a lot of duplicate effort.

For example, if you don’t want a “user” to have the Base file open, you can use a Writer document. See this post → Standalone Forms / Switchboard

I have provided the links in the original text, and added a full explanation of what I am trying to do at the end of the original text above

As noted in my comment, Base is not “Web” oriented. Anyone accessing your data will do it using an IP and not the browser. Also they need to have LO installed on their system along with your specific form and queries if designed that way. They connect to your server to get the data and nothing else.

All this about closing everything is in the hands of the user. Only item on your part is if someone is connected to the server accessing the data. You simply have the database.

Wow, that is most unfortunate, I seem to have made a fundamental design mistake, requiring a serious rethink.
I would though like to at least make my application available as a stand-alone program that can run on a PC.
So to do that, I still need to solve the questions listed at the beginning of this post.

Assume a stand-alone PC, a non-expert user of the program, who clicks on a button and runs my .odb.
Using the Sample 2 example posted by Mr Ratslinger (link), the LibreOffice GUI disappears but leaves the Form running, which the user can utilise. This is what I want.
But Ratslinger says:
"IMPORTANT! If you just close the form, the .odb is still active in memory! To Exit properly, use Ctrl & Q keys OR from Instruments menu select File->
Can a macro be written to do this automatically when the form is closed “brutally”, for example by pressing the X in the top right hand corner?

Have you looked at the link provided in the earlier comment? That is essentially running a form without opening the .odb and not needing to worry about closing a hidden version.

Simple way to go.

No, as it talked about a Writer document, which did not seem to be my case. I’ll look at it now, thanks

Yes that is correct. You may not realize this but a Base form is basically a Writer document. You can use a Writer document as a form connected to a Base file. The actual .odb need not be opened. This makes it fit in with your request.

Hi, unfortunately in my case the solution proposed using Writer does not work, due to the fact that my Form uses macros. So I am back to my original question: is it possible to write a macro to cleanly close Base when using the Sample 2 described by Ratslinger?

Not sure why that is a problem. Have used macros in standalone forms many times.

Edit:

Have removed suggested code as further testing with the hidden Base files does create problems.

Concerning the standalone Forms, which do seem the cleanest solution, and your observation that you have used macros in standalone forms many times, it seems strange that in my case the Form doesn’t work. First of all I got errors that it didn’t find the macro(s). So I copied and pasted the macros into the Form. Now it just doesn’t find any data, so probably it is not connecting to the database. I have tried going back to an old version with an embedded database, to see if that helps, but it still doesn’t find the data.

Sorry to keep posting, I may have found the solution, by manually speicfying the .odb file file as the source. Watch this space!

ok, getting there, the key is to specify the .odb file as the Data Source for all the forms and subforms. The standalone Form will though not work with the temporary tables, as the Form cannot find them in the .odb file, even if they are there! The Form also prompts me for the password to connect to the Firebird databases, so the connect_to_database macro will need looking at. So it looks to me that if I want a standalone form I will need to go back to a permanent file tbl_SrchValues to exchange data (no problem as the Form will be standalone) and to an embedded database (again no problem for the standalone Form).

Hello,

This seems to work OK although I prefer to use the standalone forms.

Create a global variable:

global Continue as boolean

At the beginning of the Sub ‘HideDBWinOpenSwitchboard’ add:

Continue = true

Just before the end of the Sub ‘HideDBWinOpenSwitchboard’ add:

do while Continue
	wait 20
loop
ThisComponent.close(true)

Add this sub:

Sub CloseBase
    Continue = false
End Sub

and link it to the Document Closed event of the form you are dealing with.

When that form is closed it will close that Base file.

Also,

Can all the code that follows be safely excluded in my case?

No. That is code to open the specific form. You must specify your form to be opened there. Of course, the commented lines can be eliminated.

Hi, I have tried the code you suggest, but on my Form it crashes LibreOffice: the Form comes up and shows the correct initialisation, but then will not respond to any input, and it crashes. If I comment out your “do while loop” then it works normally. So having a loop active in the Open Document event in my case does not seem to be a solution. I will try your suggestion of eliminating the temporary hidden table and see if that works.

Isn’t there anything that can be done just by invoking the “Document Closed” event? Thanks

Have tested this process without problem as you note. Would need to see the code you actually used.

Isn’t there anything that can be done just by invoking the “Document Closed” event? Thanks

No as the functions for the code needed is not available since the form is closed. And if done before actually closing the form it causes other problems such as crashing Base.

I have added the macro code into the original question above. It is 99% the same as yours.
By the way, I have nearly got the standalone Form to work now, just having a few problems with the emporary tables and connecting to the Firebird database (see comments just before your Answer above)

Have tested with one of your forms and your posted code. Have no problems. However, with further checking, although it completely closes the open Base file, LO is still open. A complete shutdown is not possible as there is a problem with that. See → Clean exit macro from DB and LibO ?.

I have not seen an easy solution. Here is one presented by killing the task with a shell command →
Re: Closing OpenOffice by macro

May someday test this but would need to set up a separate install to prevent damage to the system in event of problems.