How to trigger VBA Module Without Open?


I used below command for trigger macro but not working.

libreoffice --invisible --nofirststartwizard --norestore “filename.xlsm” “macro:///VBAProject.Modules.DataModule.WriteCSVFile”

Also tried,

soffice --invisible --nofirststartwizard --norestore “filename.xlsm” “macro:///VBAProject.Modules.DataModule.WriteCSVFile”

Screenshot at 2022-08-02 14-42-28

After execute above script just open file not executed expected macro script.

Hi mike,

sample file uploaded below location

password: 12345

i tried following commands:

  1. libreoffice --invisible --nofirststartwizard --norestore “sample.xlsm” “macro:///sample.xlsm.VBAProject.Modules.DataModule.WriteCSVFile”

  2. libreoffice --invisible --nofirststartwizard --norestore “sample.xlsm” “macro:///VBAProject.Modules.DataModule.WriteCSVFile”

  3. libreoffice --invisible --nofirststartwizard --norestore “sample.xlsm” “macro:///Modules.DataModule.WriteCSVFile”

  4. libreoffice --invisible --nofirststartwizard --norestore “sample.xlsm” “macro:///DataModule.WriteCSVFile”

  5. libreoffice --invisible --nofirststartwizard --norestore “sample.xlsm” “macro:///WriteCSVFile”

Not working above commands.

In the description from my link above:

You can start a macro of a specific document at its opening from a command line:

soffice path/to/test.odt macro://./standard.module1.mymacro

So the syntax for the macro URL was: macro:// + ./ (for specifying that the source is current document) + library_name.module_name.macro_name.

You never tried to use the mentioned ./; in the case when you tried to use the file name in the macro URL (your #1), you didn’t separate it from the rest using /, and you didn’t look at the next sample in the description, that shown that the file name is used without the extension. Additionally, you used that Modules. part, which is neither library name, nor module name.

The correct command would look like

soffice sample.xlsm macro://./VBAProject.DataModule.WriteCSVFile

However, note that to close the document after the macro, the macro needs to run some code itself. Also dialogs will be shown.

To allow headless operation, your document must be trusted, or the macro security level must be low (to avoid confirmation dialog); also your code must not show dialogs itself.

Thank you mike.
that command is working fine.

Another one last doubt how to pass argument parameters in headless mode.

Private Sub WriteCSVFile(Optional ByVal strPopupSkip As String = “Not Skip”)
If strPopupSkip <> “Skip” Then
MsgBox “Upload Bulk Cashback CSV Files Generated (” & CurrentTimeStamp & “)”
End If
End Sub

i tried like below not working.

libreoffice --headless --invisible --nofirststartwizard --norestore sample.xlsm macro://./VBAProject.DataModule.WriteCSVFile(“Skip”) $1

No idea why (in addition to you not using “code” formatting in the messages, so your command gets deformed by the site engine - e.g., converting "" into “”, so maybe you even have some syntax error). You didn’t provide the exact error you see - “not working” tells nothing about the error. You didn’t provide the updated XLSM, so I would have to re-create the code (which I don’t feel like doing).

We can continue this research. :slightly_smiling_face:
Works on Windows 10:

soffice C:\Temp\TestMacro.xlsx macro://./VBAProject.Module1.ShowArg("Hello world!")

How to display the classic Hello, world! I don’t know - problems with a comma.

(in the attached file, the xlsm extension has been changed to xlsx due to site restrictions).
TestMacro.xlsx (11.1 KB)

soffice C:\Temp\TestMacro.xlsx "macro://./VBAProject.Module1.ShowArg(\"Hello, world!\")"

Note that comma will have special meaning unless you put the whole argument into quotes. Then, you would need to escape the quotes inside such a quoted argument using backslash.


the Commandline:

soffice --help


   {file} {macro:///Library.Module.MacroName}                                  
                       Opens the file and runs specified macros from           
                       the file.     

It doesnt mention the dot: macro://./Lib……* to run embedded Macro!

Good catch! Could you file it as a documentation bug (I can’t go further and ask you to provide a patch :wink: )

If you give me hint howto find the source I should be able to try both.

1 Like

Actually, the very first link I posted here (tdf#104441) is such a hint: it was a bug about missing “how to run macros from command line” documentation, and the patches there add the (wrong) information to the correct place - see commits 2c9972b8d40dce72cc6f56f1d44db838bd0ece10 and 47ad5a2f2102740bda4cf02dcd73562da79f67a9, changing desktop/source/app/cmdlinehelp.cxx in core and source/text/shared/guide/start_parameters.xhp in help, respectively.

LibreOffice Information: Create a patch for LibreOffice directly in gerrit may be useful, too.

1 Like

thank you, I’ve filed #150251

1 Like