'Kundenverwaltung.odb Modul SystemTools

sub  CloseDatabase (oEv as object)
    ' macro assigned to a button on a form to close openoffice
    Dim oSvc as object
    msgbox "      Closing OpenOffice" & chr$(10) & "           Goodbye......", 0, "  --WARNING--  "
    oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
    ThisDatabaseDocument.close(true) ' >> close the internal file first
    ' pass the required parameters to execute a clean shutdown of soffice.exe
    wait 2000
    oSvc.execute("c:\windows\system32\taskkill.exe", " /F /IM soffice.exe /T" , 0) ' >> windows vista/7 syntax
End Sub

Sub DBregist            ' von geimist aus dem Forum kopiert
                        ' könnte noch optimiert und variabler gestaltet werden
    Dim aDBnames()
    Dim NeuDB As Variant
    Dim i As Integer, s As String, sFrage As String, sourceURL As String
    '========= auslesen der registrierten DBs
    DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
    aDBnames = DatabaseContext.getElementNames()
    for i = 0 to UBound(aDBnames())  'erstellen eines Stings mit Namen aller reg. DBs
        s = s & aDBnames(i) &","
    next
    If InStr(s,"Kundenverwaltung") > 0 Then        'pruefen, ob "Kundenverwaltung" vorhanden ist
        exit sub
    End if
    '========= Sicherheitsabfrage
    sFrage = "Es wurde erkannt, dass die Datenbank noch nicht in LibreOffice angemeldet ist. "+chr(13)+_
                " Wenn du auf 'JA' klickst, geschieht dies automatisch."
    If  MsgBox (sFrage,32+1,) = 2 Then
        Exit Sub
    End If       
    '========= DB automatisch registrieren
    sourceURL = ThisComponent.getUrl()
    DatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
    NeuDB=DatabaseContext.getByname(sourceURL)
    DatabaseContext.registerobject("Kundenverwaltung", NeuDB)
End Sub

Sub dbShutdown
    If not(shutdown) then
        'Variable zur Steuerung der Fensterschließung (falls false: Endlosschleife
        '(shutdown schließt Formular, Formularschließen löst dbShutdown aus etc)
        shutdown = true
        
        'Controler referenzieren
         oController = ThisDatabaseDocument.CurrentController
         oConnection = oController.ActiveConnection
         
         'Verbindung und SubComponenten schließen, Dokument speichern und DB anschließend schließen
        oDoc = ThisDatabaseDocument
        oDoc.store()
        oController.closeSubComponents
         oController.ActiveConnection.close()
        oDoc.close(True)
    endif
End sub

SUB Objekt_notifyClosing( oEvent )
  msgbox( "notify" )
END SUB

SUB Windowclose( )
    msgbox( "FensterGeschlossen" )
END SUB

SUB Formclose( )
     DIM oForm AS OBJECT
    oForm = ThisComponent.drawpage.forms.getbyindex(0)    
    MsgBox("Info: Erfolgreich gespeichert", 64, "Formular schliessen")
    oForm.reload()
END SUB

SUB CloseDatabase3 (oEv as object)
    'macro assigned to a button on a form to close openoffice
    Dim oSvc as object
    'msgbox "      Closing OpenOffice" & chr$(10) & "           Goodbye......", 0, "  --WARNING--  "
    oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
    ThisDatabaseDocument.close(true) ' >> close the internal file first
    ' pass the required parameters to execute a clean shutdown of soffice.exe
    wait 2000
    oSvc.execute("c:\windows\system32\taskkill.exe", " /F /IM soffice.exe /T" , 0) ' >> windows vista/7 syntax
End Sub

Sub CloseDatabase2
      Dim oEnum
      Dim oDoc
      ThisDatabaseDocument.FormDocuments.getbyindex(0).close
      oEnum = StarDesktop.getComponents().createEnumeration()
    Do While oEnum.hasMoreElements()
       oDoc = oEnum.nextElement()
       If HasUnoInterfaces(oDoc, "com.sun.star.util.XCloseable") Then
       oDoc.setModified(True)
       oDoc.close(True)
      Else
       oDoc.dispose()
       End If
    
    Loop
    StarDesktop.Terminate()

End Sub

SUB SaveAndCloseAll (oEv as object)
    'macro assigned to a button on a form to close openoffice
    Dim oSvc as object
    'msgbox "      Closing OpenOffice" & chr$(10) & "           Goodbye......", 0, "  --WARNING--  "
    oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
    Symbolleisten_Gast_Einblenden
    ThisDatabaseDocument.close(true) ' >> close the internal file first
    ' pass the required parameters to execute a clean shutdown of soffice.exe
    wait 2000
    oSvc.execute("c:\windows\system32\taskkill.exe", " /F /IM soffice.exe /T" , 0) ' >> windows vista/7 syntax
End Sub