Although individual extensions can be found in several places, there is currently an extension
repository at: http://extensions.libreoffice.org/ and some documentation at
http://libreplanet.org/wiki/Group:OpenOfficeExtensions/List.
For more about obtaining and installing extensions, see Chapter 14, Customizing LibreOffice.
Writing macros without the recorder
The examples covered in this chapter are created using the macro recorder and the dispatcher.
You can also write macros that directly access the objects that comprise LibreOffice if you are
confident in writing computer code. In other words, you can create a macro that directly
manipulates a document.
Directly manipulating LibreOffice internal objects is an advanced topic that is beyond the scope of
this chapter. A simple example, however, demonstrates how this works.
Listing 6: Append the text “Hello” to the current document.
Sub AppendHello
Dim oDoc
Dim sTextService$
Dim oCurs
REM ThisComponent refers to the currently active document.
oDoc = ThisComponent
REM Verify that this is a text document
sTextService = "com.sun.star.text.TextDocument"
If NOT oDoc.supportsService(sTextService) Then
MsgBox "This macro only works with a text document"
Exit Sub
End If
REM Get the view cursor from the current controller.
oCurs = oDoc.currentController.getViewCursor()
REM Move the cursor to the end of the document
oCurs.gotoEnd(False)
REM Insert text "Hello" at the end of the document
oCurs.Text.insertString(oCurs, "Hello", False)
End Sub
Finding more information
Numerous resources are available that provide help with writing macros. Use Help > LibreOffice
Help to open the LibreOffice help pages. The upper left corner of the LibreOffice help system
contains a drop-down list that determines which help set is displayed. To view the help for Basic,
choose LibreOffice Basic from this list.
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 module—some good examples include WritedbgInfo(document) and
printdbgInfo(sheet).
368 | Getting Started with LibreOffice 5.1