struggling to auto-run a macro.

Hi, thanks to Liberel I have a macro to export the list of recently used files ( Export of recently used files list?) I want to assign that macro so it autoruns but trying the Jeff’s “Tools > Customize, go to the Events tab, click on Open Document, then click the Macro button”,
I CAN assign a macro but it seems that closing LO loses the event-macro link. I have tried saving to LibreOffice and Untitled1. Any thoughts please?

What system and version? It works for me on Ubuntu 16.04 & LO 6.0.0

Hi, I am on LO Version: and Mint 18.3 ( which I believe runs on Ubuntu 16.04 LTS)

When you close LO and then reopen the document, do you see any “Assigned Action” for the Open Document event displayed in the Customize dialog ?

Also please clarify, do you want to run your macro:

a) every time that the LibreOffice application starts?
b) every time that any document is opened in LibreOffice?
c) every time that a particular document is opened in LibreOffice (  e.g. “Untitled1” )?

Hi, i do not recall seeing any Customize dialog when opening a doc after saving teh event-macro into that doc.
Also, if I try to save the Event-macro link into LibreOffice, the macro disappears from alongside the event as I do so.
In terms of when I want the macro to run, the most routine option would be to run, your, macro every time I start LibreOffice.

Hello @Boca,

As you indicated, you want to auto-run the macro every time that LibreOffice starts.
This requires connecting the “Start Application” event from “Save in: LibreOffice” ( rather than the “Open Document” event or “Save in: Untitled1” ).

You also stated that connecting the event via the GUI ( Customize Events dialog ), did not stay permanently after restarting LO.

( i’ve also had that problem once in a previous version of LO, it was resolved after i updated to a newer version ).

However in the mean time, it is also possible to connect an event without using the GUI, by calling a dedicated macro instead. ( That approach worked for me at the time when setting the same event via the GUI did not ).

So here i present the following workaround as a solution:

Assuming that you already possess the method exportRecentDocumentsList() from my previous answer, you would need the following 2 methods in addition:

  • on_ApplicationStart() – the Callback method to be connected to the LO “Start Application” event; Calls exportRecentDocumentsList();
  • connect_Start_Application_Event() – One time macro as a workaround to connect the “Start Application” event to the method on_ApplicationStart() without using the GUI.

Steps to perform:

  1. Copy-paste the 2 Basic methods from the code section below into any Module inside your Standard Basic Macro Library ( preferably into the same Module as where you stored the method exportRecentDocumentsList() before;
  2. Inside the method connect_Start_Application_Event, change the value of the Constant sModule to the name of the Module where you stored the above methods;
  3. Run the macro connect_Start_Application_Event one time, e.g. by choosing the menu “Tools : Macros : Run Macro…”, and then browse to the location where you stored the above methods, e.g. [My Macros].Standard.Module1.connect_Start_Application_Event(), and then click on the button “Run”.

If all went well with step 3), it should display a messagebox that the Event was set.

Now you can check in the menu “Tools : Customize… : Events” ( for LibreOffice ) if the Start Application event was correctly set to the method on_ApplicationStart().

After restarting LibreOffice, if everything worked alright, there should now be an exported text file present in the location that is specified inside the method on_ApplicationStart().


Sub on_ApplicationStart( oEvent As Object )
REM Should be connected to the "Start Application" event of LibreOffice.
	exportRecentDocumentsList( "$(home)/Desktop/recent_documents.txt" )
End Sub

Sub connect_Start_Application_Event()
REM Call this macro once to programmatically connect the LibreOffice "Start Application" event to the Basic macro `on_ApplicationStart()`.
	REM #****  Specify here the Name, Module, and Library of your Basic Macro to be called whenever LibreOffice starts:
	REM #****  This macro should be located inside your "[My Macros & Dialogs].Standard" library:
	REM #****  This macro should be defined with an Event Object as the first parameter:
	Const sMacro As String    = "on_ApplicationStart"	REM The Basic Macro to be called when the Application starts.
	Const sModule As String	  = "Module1"	REM The name of the Basic Module that contains the above Macro.
	Const sLibrary As String  = "Standard"	REM The name of the Basic Library that contains the above Module.

	Dim aProps(1) As New
	aProps(0).Name		= "EventType"
	aProps(0).Value		= "Script"
	aProps(1).Name		= "Script"
	aProps(1).Value		= "" & sLibrary & "." & sModule & "." & sMacro & "?language=Basic&location=application"
	Dim oGlobalEventBroadcaster As Object
	oGlobalEventBroadcaster = GetDefaultContext().getByName( "/singletons/" )
	oGlobalEventBroadcaster.Events.replaceByName( "OnStartApp", aProps() )
	Msgbox "Application Event Connected: <OnStartApp> :--->  " &  sLibrary & "." & sModule & "." & sMacro
End Sub

With Regards, lib

Thanks lib, I’ll check into this asap.