• Polski
  • Español
  • Français
  • English
  • 日本語
  • Português
  • Deutsch
  • 中文 (中国)
  • LibreOffice from Scratch – Part 8: Macros and Automation

    Do you want LibreOffice to perform repetitive tasks for you? With macros, it’s possible! In this part, you will learn the basics of automation—from creating your first macro, assigning it to a button, to a brief introduction to Python macros.

    You don’t need to be a programmer to get started. LibreOffice Basic allows you to automate actions that would normally take minutes—with just one click.

    What are macros and how do they work?
    Macros are mini-programs that automate tasks in LibreOffice—such as formatting a document, copying data, filling cells, or generating a report.

    LibreOffice supports macros in several languages, with LibreOffice Basic being the simplest and best integrated with the suite.

    Macros can be recorded or written manually—it all depends on your level of experience.

    Creating your first macro
    Go to: Tools → Macros → Edit Macros.
    You will be taken to the LibreOffice Basic editor.
    Paste a simple code:

    Sub HelloWorld
        MsgBox "Hello, LibreOffice!"
    End Sub

    Save and run it—a message box will appear.

    Safe execution and assigning macros
    Macros can make changes to files, which is why LibreOffice blocks them by default.
    Go to: Tools → Options → LibreOffice → Security → Macro Security and set the trust level.
    You can also add a trusted file location.

    How to assign a macro to a button:
    Insert a button from the Form Controls toolbar.
    Right-click → Control Properties → Events tab.
    Choose, for example, On Click, and assign the HelloWorld macro.

    Macros in Python (optional)
    LibreOffice also supports macros in Python, which provides access to more advanced features and integrations.

    Example:

    def greetings():
        doc = XSCRIPTCONTEXT.getDocument()
        doc.Text.insertString(doc.Text.End, "Hello from Python!\n", 0)

    Note: Python macros require slightly different environment setup—more on that in a separate article.

    In this part:

    you learned what macros are,
    you created your first macro in Basic,
    you learned how to run them safely and assign them to buttons,
    you saw that Python is also an option.

    Macros help save time and simplify work. In future guides, you can expand them into fully automated solutions!