Operation Manual
Introduction
A macro is a saved sequence of commands or keystrokes that are stored for later use. An example
of a simple macro is one that “types” your address. The LibreOffice macro language is very
flexible, allowing automation of both simple and complex tasks. Macros are very useful when you
have to repeat the same task in the same way over and over again.
LibreOffice macros are usually written in a language called LibreOffice Basic, sometimes
abbreviated to Basic. Although you can learn Basic and write macros, there is a steep learning
curve to writing macros from scratch. The usual methods for a beginner are to use macros that
someone else has written or use the built-in macro recorder, which records keystrokes and saves
them for use.
Most tasks in LibreOffice are accomplished by “dispatching a command” (sending a command),
which is intercepted and used. The macro recorder works by recording the commands that are
dispatched (see “Dispatch framework” on page 352).
Your first macros
Adding a macro
The first step in learning macro programming is to find and use existing macros. This section
assumes that you have a macro that you want to use, which may be in an email, on a web page, or
even in a book. For this example, the macro in Listing 1 is used. You must create a library and
module to contain your macro; see “Macro organization” on page 353 for more information.
Listing 1: Simple macro that says hello
Sub HelloMacro
Print "Hello"
End Sub
Use the following steps to create a library to contain your macro:
1) Use Tools > Macros > Organize Macros > LibreOffice Basic to open the LibreOffice
Basic Macro dialog (Figure 332).
2) Click Organizer to open the Basic Macro Organizer dialog (Figure 333) and select the
Libraries tab.
3) Set the Location to My Macros & Dialogs, which is the default location.
4) Click New to open the New Library dialog.
5) Enter a library name, for example TestLibrary, and click OK.
6) Select the Modules tab.
7) In the Module list, expand My Macros and select, for example TestLibrary. A module named
Module1 already exists and can contain your macro. If you wish, you can click New to
create another module in the library.
8) Select Module1, or the new module that you created, and click Edit to open the Integrated
Debugging Environment (IDE) (Figure 334). The IDE is a text editor included with
LibreOffice that allows you to create and edit macros.
9) When a new module is created, it contains a comment and an empty macro named Main,
which does nothing.
10) Add the new macro either before Sub Main or after End Sub. Listing 2 shows the new
macro has been added before Sub Main.
342 | Getting Started with LibreOffice 5.0