This is where the problem starts…
waits for the user to complete his/her work via an open form. When the user closes the form, a flag is switched that closes the application
Anything that prevents the Open Document event to finish running can cause a crash or become unresponsive.
Closing the document in the Open Document event or before the event finishes can do the same.
Sample database:
TestCloseDatabase.odb (13.5 KB)
The code in the sample database for hiding, minimizing/disabling and opening the form is commented out. This will allow testing the form to ensure it works with your OpenOffice/LibreOffice installation and operating system.
The code has only been tested in windows with AOO/LIBO version 4.1 and higher.
Your mileage may vary for other AOO/LIBO installations and operating systems.
If the only office application open is your database, closing the form will close the database without opening the start center thereby completely closing the office.
If there are other applications (writer, calc, base, ect) open, closing the form will only close the database.
The open document event presents 2 options.
Hide the base application…
Or
Minimize and disable the base application.
Pros and cons
If base is hidden users will not see or can interact with the base window. However, if there is 1 or more other office applications open, closing the last other application will also close the hidden base application. Which might not be desirable.
If base is minimized and disabled, closing other applications will not close the database.
Users can restore, maximize move and size the base window. However, they will not be able to interact with the base window nor will they be able to close it.
Macros for closing base
Sub CloseComponent(poComponent As Object)
'poComponent ThisDatabaseDocument
Dim oComponentsEnum As Object, oComponent As Object
Dim i As Long, sUNO As String
oComponentsEnum = StarDesktop.getComponents().createEnumeration()
If (Not IsNull(oComponentsEnum)) Then
Do While oComponentsEnum.hasMoreElements()
On Local Error Goto errLocal
oComponent = oComponentsEnum.nextElement()
If Not isDatabaseComponent(oComponent) Then
i = i + 1
End If
errLocal:
On Local Error Goto 0
Loop
End If
'Close the connection here if needed
'The application window must be visible
'poComponent.CurrentController.Frame.ContainerWindow.setVisible(True)
' OR
'The application window must be enabled
poComponent.CurrentController.Frame.ContainerWindow.setEnable(True)
Wait 300
If i = 1 Then
sUNO = ".uno:Quit"
Else
sUNO = ".uno:CloseDoc"
End If
unoQuitCloseDoc(poComponent, sUNO)
End Sub
Function getModuleIdentifier(oComp As Object) As String
Dim oModuleMgr As Object
oModuleMgr = createUnoService("com.sun.star.frame.ModuleManager")
getModuleIdentifier = oModuleMgr.identify(oComp)
End Function
Function isDatabaseComponent(oComp As Object) As Boolean
Dim sIdentifier As String
sIdentifier = getModuleIdentifier(oComp)
If sIdentifier = "com.sun.star.report.ReportDefinition" Then isDatabaseComponent = True : Exit Function
If Instr(sIdentifier, ".sdb") > 0 Then
If sIdentifier <> "com.sun.star.sdb.OfficeDatabaseDocument" Then
isDatabaseComponent = True
End If
End If
End Function
Sub unoQuitCloseDoc(oDoc As Object, sUNO As String)
Dim oParser As Object, oFrame As Object, oDispatcher As Object
Dim aURL As New com.sun.star.util.URL
Dim arArgs() As New com.sun.star.beans.PropertyValue
If IsNull(oDoc) Then Exit Sub
If sUNO <> ".uno:Quit" Then
If sUNO <> ".uno:CloseDoc" Then Exit Sub
End If
oDoc.setModified(False)
oParser = createUnoService("com.sun.star.util.URLTransformer")
aURL.Complete = sUNO
oParser.parseStrict(aURL)
oFrame = oDoc.CurrentController.Frame
oDispatcher = oFrame.queryDispatch(aURL, "_self", com.sun.star.util.SearchFlags.NORM_WORD_ONLY)
oDispatcher.dispatch(aURL, arArgs())
End Sub