How to Run a LibreOffice Macro from Command Line Without GUI

command linelibreofficemacros

I want to run a LibreOffice macro on .odt file(s) from the command line. Because I want to scale this up to applying the macro to multiple files, I don't want the GUI to pop-up in each execution of the macro.

I currently have a working macro (that also closes the file at the end) and as far as I can find, I should be able to call it as followed:

soffice --invisible --nofirststartwizard --headless --norestore "D:\myFolder\my file.odt" "macro:///Standard.Module1.myMacro"

or

swriter --invisible --nofirststartwizard --headless --norestore "D:\myFolder\my file.odt" "macro:///Standard.Module1.myMacro"

Both commands execute the macro correctly, however the GUI opens and closes during execution. How do I prevent this?

I am working on a Windows 10 Computer and Help>About LibreOffice gave the following info:

Version: 5.2.1.2
Build ID: 31dd62db80d4e60af04904455ec9c9219178d620
CPU Threads: 4; OS Version: Windows 6.2; UI Render: default;
Locale: en-US (en_US); Calc: CL

Best Answer

The problem is that even though LibreOffice is invisible when started, it becomes visible after opening a document. There is a solution at https://forum.openoffice.org/en/forum/viewtopic.php?f=5&t=22548:

  1. Run LibreOffice headless to call a macro. The command line call should not specify the document to open, just a macro. For example (using the newer macro syntax):

    soffice -headless -invisible "vnd.sun.star.script:Standard.Module1.MySubroutine?language=Basic&location=application"

  2. The macro calls loadComponentFromUrl with the Hidden property set to true. This will cause the document to not become visible.

  3. Now the macro performs whatever it was going to do with the document.

EDIT:

To make it work for different files, pass the filename as a parameter using the older macro syntax. An example from https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232:

soffice "macro:///Library3.Module1.test_Args(arg1,123,4.567,2000-12-31)"
Related Question