Ask Your Question
1

struggling to auto-run a macro.

asked 2018-02-06 22:36:22 +0200

Boca gravatar image

updated 2018-02-06 22:40:32 +0200

Hi, thanks to Liberel I have a macro to export the list of recently used files ( https://ask.libreoffice.org/en/questi...) 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?

edit retag flag offensive close merge delete

Comments

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

Pansmanser gravatar imagePansmanser ( 2018-02-06 23:19:06 +0200 )edit

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

Boca gravatar imageBoca ( 2018-02-06 23:34:13 +0200 )edit

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 ?

librebel gravatar imagelibrebel ( 2018-02-06 23:48:36 +0200 )edit

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” )?
librebel gravatar imagelibrebel ( 2018-02-07 01:45:29 +0200 )edit

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. Thanks.

Boca gravatar imageBoca ( 2018-02-07 10:05:09 +0200 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2018-02-07 21:28:18 +0200

librebel gravatar image

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().

code

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
    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 ...
(more)
edit flag offensive delete link more

Comments

Thanks lib, I'll check into this asap.

Boca gravatar imageBoca ( 2018-02-07 21:52:13 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-02-06 22:36:22 +0200

Seen: 500 times

Last updated: Feb 07 '18