Is it possible to automatically open most recent document on start?

When opening LibreWriter, can I set it to automatically open most recent doc?

1 Like

Hello @user2635,

Yes, if you can assign a custom macro to the “Start Application” event via the menu "Tools : Customize : Events", then it is possible.

i was working on an example to accomplish this, when i found out that my own “Start Application” event is mysterously missing…

So i cannot test if my example macro works, before i upload it.

Found a workaround for my missing “Start Application” event (see here).

Will post a solution shortly…

Hello @user2635,

To automatically open your most recent document each time that LibreOffice starts up, you could do this :

  1. Copy-paste the code below to a module called “LibreOffice” inside your Standard Basic library.
    If your [My Macros & Dialogs].Standard does not yet have a module called “LibreOffice”, then create it.
  2. Call the method [My Macros & Dialogs].Standard.LibreOffice.set_Start_Application_Event_To_Open_Most_Recent_Document() once.
    ( i.e. by choosing the menu “Tools : Macros : Run Macro…”, then browse to the method and click Run )
    A messagebox will inform you if the connection succeeded.

Ready, LO will now open your most recent document each time it starts up.
HTH, lib

Sub set_Start_Application_Event_To_Open_Most_Recent_Document()
REM Programmatically connect the "Start Application" event to the Basic method called: Open_Most_Recent_Document().
REM NB.
REM Make sure that the Basic method called "Open_Most_Recent_Document" exists inside [My Macros & Dialogs],
REM inside the library called "Standard", inside the module called "LibreOffice".
REM Then call this method once to connect the event.
REM NB. 
REM This causes the Most Recently opened Document to be opened *EVERY TIME* that the LO application starts up.

	set_Application_Event( "OnStartApp", "Standard", "LibreOffice", "Open_Most_Recent_Document" ) 
End Sub


Sub set_Application_Event( sEventName as String, sLibrary As String, sModule As String, sFunction As String )
REM Connects the specified Application Event to the specified Basic macro.
REM <sEventName>: The Name of the Application Event to connect a macro to.
REM <sLibrary>	: The Name of the Library that contains the module <sModule>.
REM <sModule>	: The Name of the Module that contains the method <sFunction>.
REM <sFunction>	: The Name of the Method to be connected to the specified Application Event.

	Dim aProps(1) As New com.sun.star.beans.PropertyValue
	aProps(0).Name		= "EventType"
	aProps(0).Value		= "Script"
	aProps(1).Name		= "Script"
	aProps(1).Value		= "vnd.sun.star.script:" & sLibrary & "." & sModule & "." & sFunction & "?language=Basic&location=application"
	
	Dim oGlobalEventBroadcaster As Object
	oGlobalEventBroadcaster = GetDefaultContext().getByName( "/singletons/com.sun.star.frame.theGlobalEventBroadcaster" )
	oGlobalEventBroadcaster.Events.replaceByName( sEventName, aProps() )
	msgbox sEventName & " connected to: " & oGlobalEventBroadcaster.Events.getByName( sEventName )(1).Value, 64, "Set Application Event"
End Sub


Function Open_Most_Recent_Document()
REM Opens the Most Recently opened LibreOffice Document, and returns it if succesful.
	On Local Error Resume Next
	Open_Most_Recent_Document = StarDesktop.loadComponentFromURL( getMostRecentDocument(), "_blank", 0, Array() )
End Function


Function getMostRecentDocument() As String
REM Returns the URL of the most recently opened document in LibreOffice.
REM Returns an empty string if there are no recent documents.
	Dim aRecentDocs() As String
	aRecentDocs = getRecentDocuments()
	If uBound( aRecentDocs ) >= 0 Then getMostRecentDocument = aRecentDocs( 0 )
End Function


Function getRecentDocuments( Optional bOrdered As Boolean ) As Variant
REM	Returns an Array containing the URLs of LibreOffice Most Recent Documents.
REM <bOrdered>	: Pass <True> ( or leave empty ) to get the array sorted by most recent date;
REM				  Pass <False> to get the unordered List of Recent Documents.
	Dim aProps(1) As New com.sun.star.beans.PropertyValue
	aProps(0).Name	= "nodepath"
	aProps(0).Value	= "/org.openoffice.Office.Histories/Histories/"
	aProps(1).Name	= "enableasync"
	aProps(1).Value	= False
	
	Dim oConfig As Object, oHistory As Object
	oConfig		= createUnoService( "com.sun.star.configuration.ConfigurationProvider" )
	oHistory	= oConfig.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationAccess", aProps() )

	If IsMissing( bOrdered ) Or bOrdered Then		  REM Create ordered item list:
	
		Dim oOrderList		As Object	:	oOrderList	= oHistory.PickList.OrderList
		Dim sOrderNames()	As String	:	sOrderNames	= oOrderList.getElementNames()
		Dim iMaxIndex		As Integer	:	iMaxIndex	= uBound( sOrderNames )
		Dim aOrderedList( iMaxIndex ) As String
		Dim i As Integer
		
		For i = 0 To iMaxIndex
			aOrderedList( i ) = oOrderList.getByName( sOrderNames(i) ).HistoryItemRef
		Next
		
		getRecentDocuments = aOrderedList()
	Else
	
		getRecentDocuments = oHistory.PickList.ItemList.getElementNames()
	End If
End Function
1 Like

I see, thanks but I should’ve actually added the word ‘simple’ to my request. Is there a ‘simple’ way to make Writer opened most recent doc (via a checkbox in settings)? Are you aware if this is already reported as an improvement?

Unfortunately i do not know if this feature is going to be implemented in LibreOffice any time soon.

The provided solution is already as simple as i could make it in these circumstances, it requires only 2 steps to install it.

It would be easy to connect this function to a custom checkbox button, menuitem, or keyboard shortcut, but it would require some additional steps to perform.

1 Like