1

In a python script, I know I can execute a custom libreoffice macro in the My Macros library container by grabbing a dispatcher and executing:

dispatcher.executeDispatch(frame, "macro:///Standard.Module1.mymacro", "", 0, ())

But how do I call a local document specific macro? I presume there is a replacement term for "macro///" such as perhaps "document///" that would make the call above work but I cannot find any documentation anywhere. What is the correct format for the macro call string?

Galo do Leste
  • 703
  • 5
  • 13
  • Good question. If you're trying to call another function from a Python script, and you are in Windows, I have found that you can drop those files inside the Program Files\LibreOffice\program directory and it is possible to import. – bfris Jun 18 '20 at 06:08

1 Answers1

3

Get the script provider from the document and use it to invoke the macro.

desktop = XSCRIPTCONTEXT.getDesktop()
file_url = uno.systemPathToFileUrl("C:/path/to/file.odt")
doc = desktop.loadComponentFromURL(file_url, "_blank", 0, ())
oScriptProvider = doc.getScriptProvider()
oScript = oScriptProvider.getScript(
    "vnd.sun.star.script:Standard.Module1.mymacro?"
    "language=Basic&location=document")
oScript.invoke((), (), ())

Adapted from Execute LibreOffice Calc Basic macro from python.

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • Thank you. Worked like a charm. While not necessarily obvious it certainly makes a lot of sense now. Some clarification for others that might help. If the python script is run from an embedded python script (as mine was), the loadComponentFromURL will open another copy of the odt file. This could cause issues with two copies of file open. This is fixed by removing line 2 and three of respone and editing line 1 to: doc=XSCRIPTCONTEXT.getDocument() – Galo do Leste Jun 19 '20 at 01:59