Search code examples
basiclibreoffice-base

How to finish a loop when closing form(window)


This should be really easy but I can't find it out. Im trying to do a loop for saving the form records every minute. Which works but when I close the form the macro keeps running and eventually returns a unwanted error because form is no longer open:

BASIC runtime error.
An exception occurred 
Type: com.sun.star.lang.DisposedException
Message: Frame disposed. 

This is my macro

Sub Save_loop
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

   Do Until   '???window is closed
       dispatcher.executeDispatch(document, ".uno:RecSave", "", 0, Array())
       wait 60000
   Loop
End Sub

What can I put on the While/Until for not returning this error?


Solution

  • userForm_QueryClose() event should work

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
       is_closed = 1
    End Sub
    
    
    Sub Save_loop
       document = ThisComponent.CurrentController.Frame
       dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    
           Do Until   '???window is closed
               If (is_closed = 1) Then break
               dispatcher.executeDispatch(document, ".uno:RecSave", "", 0, Array())
               wait 60000
           Loop
    End Sub