Académique Documents
Professionnel Documents
Culture Documents
Sometimes the macro recorder has a failure and understanding LibreOffice internal workings helps
to understand how and why the macro recorder sometimes fails. The primary offender is related to
the dispatch framework and its relationship to the macro recorder.
Dispatch framework
The purpose of the dispatch framework is to provide uniform access to components (documents)
for commands that usually correspond to menu items. Using File > Save from the main menu bar,
the shortcut keys Ctrl+S, or clicking the Save icon are all of commands that are translated into the
same dispatch command.
The dispatch framework can also be used to send commands back to the User Interface (UI). For
example, after saving a new document, the list of recent files is updated.
A dispatch command is text, for example .uno:InsertObject or .uno:GoToStartOfLine. The
command is sent to the document frame and this passes on the command until an object is found
that can handle the command.
Other options
When the macro recorder is not able to solve a specific problem, the usual solution is to write code
using the LibreOffice objects. Unfortunately, there is a steep learning curve for these LibreOffice
objects. It is usually best to start with simple examples and then increase the scope of macros as
you learn more. Learning to read generated macros is a good place to start.
If you record Calc macros, and the recorder can correctly generate a macro, there is an add-in
available which converts Calc macros when they are recorded. The final code manipulates
LibreOffice objects rather than generating dispatches. This can be very useful for learning the
object model and can be downloaded directly from the web site:
http://www.paolo-mantovani.org/downloads/DispatchToApiRecorder/
Caution
LibreOffice allows you to import libraries into a library container, but it will not allow you
to overwrite the library named Standard. Therefore, if you store your macros in the
Standard library, you cannot import them into another library container.
Importing macros
The LibreOffice Macro Organizer dialog allows you to import macro libraries into your document as
well as creating, deleting, and renaming libraries, modules, and dialogs.
1) Select the library container to use and then click Import to import macro libraries (Figure
341 on page 352).
2) Navigate to the directory containing the library to import (Figure 346). There are usually two
files from which to choose, dialog.xlb and script.xlb. It does not matter which of these two
files you select; both will be imported. Macros can be stored in libraries inside LibreOffice
documents. Select a document rather than a directory on disk to import libraries contained
in a document.
Note
You cannot import the library named Standard.
Tip
On a computer operating Linux, the LibreOffice specific files are stored in the home
directory of a user, in a subdirectory whose name begins with a period (usually .config/).
Directories and files with names beginning with a period may be hidden and not shown
in a normal selection dialog. If using LibreOffice dialogs, rather than the operating
system specific dialogs, type the name of the desired directory in the Name field.
3) Select a file and click Open to continue and open the Import Libraries dialog (Figure 347).
Events
Whenever something happens in LibreOffice, it is called an event. For example, opening a
document, pressing a key, or moving the mouse cursor are all events. LibreOffice allows events to
trigger the execution of a macro; the macro is then called an event handler. Full coverage of event
handlers is well beyond the scope of this document, but a little knowledge can accomplish much.
Caution
Be careful when you configure an event handler. For example, assume that you write
an event handler that is called every time that a key is pressed, but you make a
mistake so the event is not properly handled. One possible result is that your event
handler will consume all key presses, forcing you to kill LibreOffice.
1) Go to Tools > Customize on the main menu bar to open the Customize dialog and select
the Events tab (Figure 349). The events in the Customize dialog are related to the entire
application and specific documents.
2) In Save In, select LibreOffice, or a specific document from the drop down menu to save
your event.
Many objects in a document can be set to call macros when events occur. The most common use
is to add a control, such as a button, into a document. Even double-clicking on a graphic opens a
dialog with a Macros tab that allows you to assign a macro to an event.
Extensions
An extension is a package that can be installed into LibreOffice to add new functionality.
Extensions can be written in almost any programming language and may be simple or
sophisticated. Extensions can be grouped into types:
Calc Add-Ins, which provide new functionality for Calc, including new functions that act like
normal built-in functions
New components and functionality, which normally include some level of User Interface (UI)
integration such as new menus or toolbars
Pivot Tables that are used directly in Calc
Chart Add-Ins with new chart types
Linguistic components such as spelling checkers
Document templates and images
Sub AppendHello
Dim oDoc
Dim sTextService$
Dim oCurs
Included material
Many excellent macros are included with LibreOffice. Use Tools > Macros > Organize Macros >
LibreOffice Basic to open the Macro dialog. Expand the Tools library in the LibreOffice library
container. Inspect the Debug modulesome good examples include WritedbgInfo(document) and
printdbgInfo(sheet).
Note
The menu bars in LibreOffice 5.1 Writer, Calc, and Impress have been reorganized to
improve the usability of the application. The most used application commands were put
together in the Styles, Sheets, and Slide menus, respectively.
Note
Customizations to menus and toolbars can be saved in a template. To do so, first save
them in a document and then save the document as a template as described in
Chapter 3, Using Styles and Templates.
Note
It is possible to use a letter already in use in the menu list (for example, in the Insert
menu, the letter v is used in Envelope and in Movie and sound as an accelerator).
However, you should use an unused letter if possible, to make it simpler to navigate.
Customizing toolbars
You can customize toolbars in several ways, including choosing which icons are visible and locking
the position of a docked toolbar (as described in Chapter 1, Introducing LibreOffice), and adding or
deleting icons (commands) in the list of those available on a toolbar. You can also create new
toolbars. This section describes how to create new toolbars and add or delete icons on existing
ones.
To get to the toolbar customization dialog (Figure 353), do any of the following:
On the toolbar, right-click in the toolbar and choose Customize Toolbar.
Choose View > Toolbars > Customize from the menu bar.
Choose Tools > Customize from the menu bar and go to the Toolbars page.
Caution
Be careful when reassigning your operating systems or LibreOffices predefined
shortcut keys. Many key assignments are universally understood shortcuts, such as F1
for Help, and are always expected to provide certain results. Although you can easily
reset the shortcut key assignments to the LibreOffice defaults, changing some common
shortcut keys can cause confusion, especially if other users share your computer.
To adapt shortcut keys to your needs, use the Customize dialog, as described below.
1) Select Tools > Customize > Keyboard. The Customize dialog opens.
2) To have the shortcut key assignment available in all components of LibreOffice select the
LibreOffice button.
3) Next select the required function from the Category and Function lists.
Note
All existing shortcut keys for the currently selected Function are listed in the Keys
selection box. If the Keys list is empty, it indicates that the chosen key combination is
free for use. If it were not, and you wanted to reassign a shortcut key combination that
is already in use, you must first delete the existing key.
Shortcut keys that are grayed-out in the listing on the Customize dialog, such as F1
and F10, are not available for reassignment.
2) To have the shortcut key assignment available only with one component (for example,
Writer), select that components name in the upper right corner of the page; otherwise
select LibreOffice to make it available to every component.
Installing extensions
Extensions can be installed in any of these ways:
Directly from the *.oxt file in your systems file browser.
From your web browser, if it can be configured to open this file type from a web page
hyperlink.
Directly from Tools > Extension Manager by clicking Add (Figure 359).
To install directly from a saved *.oxt file on your system, double-click the file.
To install from a suitably enabled web browser, select the hyperlink, and then select to Open the
file.
In both cases, after the Extension Manager opens, a warning dialog opens for you to verify the
installation and then continue. The file is installed as a user only file in a multi-user system.
To install directly from Extension Manager:
1) In LibreOffice, select Tools > Extension Manager from the menu bar. The option to view
the extensions bundled with LibreOffice can be deselected to view more easily those
installations that have been added by a user.
2) In the Extension Manager dialog (Figure 359), click Add.
3) A file browser window opens. Find and select the extension you want to install and click
Open.
4) Users with administrator or root privileges will see a dialog where they can choose to install
extensions for all users (shared) or only for me (user). Normal users without those
privileges can install, remove, or modify extensions only for their own use (user).
5) The extension begins installing.
In all cases, during the process you may be asked to accept a license agreement. When the
installation is complete, the extension is listed in the Extension Manager dialog.
Tip
To get extensions that are listed in the repository, you can open the Extension Manager
and click the Get more extensions online link. You do not need to download them
separately.