Macro call document close, Libreoffice Crash Send Report after reopen

hello,
i ve written some small macro that save and close the document after x minutes.
the document get closed, but next start time the crash report is opened.
i try several methods to save the document, but always the same.

The button toggle an not visible flag,
the macro is called after open the document.

Sub toggle_auto_exit_timer

	oBreak = ThisComponent.CurrentController.ActiveSheet.DrawPage.Forms.getByIndex(0).getByName("autoclose_exit")
	if oBreak.State > 0 then
		oBreak.State = 0
	else
		oBreak.State = 1
	end if
End Sub

Sub auto_exit_timer

	DIM timer as string
	DIM oDocument as object
	DIM auto_close_time as string
	
	auto_close_time = "00:00:20"
	start_time = Time
	end_time = TimeValue(start_time) + TimeValue(auto_close_time) 
	oDocument = ThisComponent

	oButton = oDocument.CurrentController.ActiveSheet.DrawPage.Forms.getByIndex(0).getByName("Autoclose")
	oBreak = oDocument.CurrentController.ActiveSheet.DrawPage.Forms.getByIndex(0).getByName("autoclose_exit")
	oBreak.State = 0
	
	While Time() < TimeValue(end_time)	
		if oBreak.State > 0 then
			oButton.Label = "Autoclose disabled"
			while(oBreak.State > 0)
				wait(1000)
			Wend
			start_time = Time
			end_time = TimeValue(start_time) + TimeValue(auto_close_time) 	
		else	
			diff = TimeValue(end_time) - TimeValue(Time())
			timer = ""
			if(SECOND(diff) < 10) then
				timer = "0"
			endif
				timer = timer +  + SECOND(diff)
			timer =  MINUTE(diff) & ":"  & timer
			oButton.Label = "Autoclose in " + timer
			Wait 1000
		end if
	Wend
	
	oDocument.Store
	oDocument.Close(1)
	
End Sub

i although tried:

	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	dispatcher.executeDispatch(oDocument, ".uno:Save", "", 0, Array())
       oDocument.close

Windows 10 Libreoffice 7.4.2 and 7.4.1.2 tested

how can i save and close the document without get an crash report,
and without get closed other opened documents?

thanks a lot
help is aprricated :slight_smile:

Where your macro is stored? In the document or under the MyMacros in the User profile of the office suite?

the macros are local stored NOT in the document.

can you post your calc document?

hello,
here is the document, but the macros are not included :slight_smile:
autoclose.ods (11.4 KB)

Hi, I can reproduce your issue. When starting the auto_exit_timer routine through the menu / macro / execute, everything runs fine. It seems that the problem is due to assigning the auto_exit_timer routine to the OnLoad event.
I find a similar problem on Apache OO. There, AO is not killed, but starts the next time with the recovery process.
Sorry, I cannot be of help here. Sounds like a issue deeply rooted in AOO / LO

Good luck,

ms777

I repro using 7.5.1, but it seems to work OK in current master towards 7.6, so likely fixed at some point.

Hi,
a workaround is going through a python script. Python allows for multithreading, and the problem does not occur when document.close() is called from a different thread

Install the below macro into your LibreOffice python script directory, name it e.g. autoclose.py

Assign the document open event to autoclose.py$closeDocument

import threading

ctx = XSCRIPTCONTEXT.getComponentContext() # type: ignore

def closeDocumentHelper(doc1):
    mspf = ctx.ServiceManager.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", ctx)
    script_provider = mspf.createScriptProvider("")
    oScript = script_provider.getScript('vnd.sun.star.script:Standard.Test.auto_exit_timer?language=Basic&location=application')
    oScript.invoke((), (), ())
    doc1.close(True)

def closeDocument(ev):
    t = threading.Thread(target = closeDocumentHelper, args = (ev.Source,))
    t.start()

Good luck,

ms777

Then the only question that remains is why a basic function is still involved here at all via seven corners.

import threading
from datetime import datetime as dt, timedelta
from time import sleep

def autosave30(doc):
    jetzt = dt.now()
    plus30 = jetzt + timedelta(seconds=30)
    while plus30-dt.now().seconds > 0:
        sleep(1)
    doc.store()
    doc.close(True)


def closeDocument(ev):
    t = threading.Thread(target =autosave30 , args = (ev.Source,))
    t.start()

…it is only two or three corners :grin:
Of course it would be more beautiful to write everything in Python. But I assumed that the OP is only capable of Basic, and I was too lazy to translate everything. I wasn’t aware that is so quick as in your code
Cheers ms777

Unfortunatly the whole thing doesnt work, if the Code is embedded into the Document itself, so you need to store into $user-config/Scripts/python!

Broken down to a minimum example:

from threading import Timer


def autoclose(event): 
    
    def job(doc):
        doc.store()
        doc.close(True)
        
    t = Timer(30.0, job, args=(event.Source,))
    t.start()
    

hi,
thanks for the answer.
using python is a solution, but i think the behaviour is a bug.
should i report the bug at bugzilla documentfoundation?
thanks

Its your decision…(IHMO) this autoclose does not make sense at all.

yes,
it make sense. it is an order document and sometimes a person forget to close the document and than it is blocked. now the document closed automatic after 5 minutes,
and the document is not blocked all the time.