LibreOffice Macro Organization


This tutorial will share the basic idea of LibreOffice Macro file structures and organizations which can be used to export or share LibreOffice Macro as part of the office file.

If you are familiar with M$ Excel VBA or VBA in general, you would know that the VB codes of a macro reside in a VB project and if you want to include the macro inside the excel for example, you have to save the file as .xlsm format which tells that the file contains macros.

LibreOffice Macro Organization

LibreOffice Macro File Default Structures

Well, for LibreOffice, the process is definitely possible and it is organized in a different matter.
When you first create a Macro, by default LibreOffice creates it under a path : My Macros -> Standard -> Module1. All of your macro functions are resides under Module1. For this tutorial, I have created a function my_macro that resides under Module1.

LibreOffice 4 keeps all the macro codes that are created under the default Module1 in path /home/[user name]/.config/libreoffice/4/user/basic/Standard.

Inside the path, there should be a file called Module1.xba which contains your actual default macro functions. The test function my_macro and its contents should be inside this file.

How to Create Macros as part of the Office File

If you have written lots of macros and functions using the above default file structure and save the file, the macros will not be saved along with the same file. If you distribute or send this file to someone else, they will not see the macros when they open the file. Because the macro contents remain in the disk where it has been coded/developed.

If you want to distribute or send the LibreOffice file (Calc, Impress, etc) with the macro functions, modules you have to create the macros in a different way.

While creating macros and dialog which you want to distribute as part of the LibreOffice file, you have to create the macro inside the file. Open the dialog from Tools -> Macros -> Organize Macros -> LibreOffice Basic. Inside the dialog, select the file name on the left side under the label ‘Macro From’. Press ‘New’, LibreOffice will ask for a module name.

As default “Module1” is already in use by LibreOffice, type any name you want and press OK (I have used debugpoint_demo_module as module name for this tutorial).

Once that is done, the macro editor will open with your new module and macro under your working file. Now you can write as many macros you want under the current module and save the working LibreOffice file. Once the file is saved, all the macros and their contents are saved along with the file and you can easily share or distribute the file to users.

Containers – Libraries – Modules – Macros

Macros are stored under Modules in LibreOffice. Modules are part of Libraries and Libraries are part of containers as shown in the below image.

Loading the user-defined Module

By default, LibreOffice assumes all the dialogs, variables are from “Module1” as per default organization. But we have created a new module named “debugpoint_demo_module” which remains along with the file itself. You need to load the module before you start writing macro codes. You can load the user-defined modules using the below method:

ThisComponent.BasicLibraries.LoadLibrary("debugpoint_demo_module")

Note:
If you also created a dialog – say “my_dialog” under the working file itself, you can create the dialog object using below:

Dim oDialog1 As Object
oDialog1 = CreateUnoDialog(DialogLibraries.Standard.my_dialog)

Enabling Macro in LibreOffice Files

After you distribute or share the LibreOffice file with the macro in it, the user may face the below error when opening the file in the corresponding system. Macro execution is disabled by default in LibreOffice due to security reasons.


As instructed, go to Tools -> Options -> LibreOffice -> Security -> Macro Security. By default, the macro security is set to ‘high’. You can mark it as ‘low’ if you trust the source of the macro and save. Then you can try executing the macro.


Conclusion

This summarises how to create your own macro and save it along the file to distribute. If you are facing a problem following the above tutorial drop a comment using the comments box below. For more LibreOffice macro tutorials, go to the top menu and find the macro tutorial index.

 

Looking for Something Else?

If you are looking for something else in LibreOffice macro tutorials, Or, wants to learn more about it, please follow the below link for a complete Macro Tutorials Index:

LibreOffice Macro Tutorial Index

Exit mobile version