Operation Manual

Another kind of subroutine is called a function, which is a subroutine that returns a value.
Functions are defined by the keyword FUNCTION at the beginning. However, recorded
macros in LibreOffice always create subroutines, not functions.
Defining variables using Dim
You can write information on a piece of paper so that you can look at it later. A variable, like a piece
of paper, contains information that can be changed and read. The Dim keyword originally stood for
Dimension and was used to define the dimensions of an array. The dim statement used in the
EnterMyName macro is similar to setting aside a piece of paper to be used to store a message or
note.
In the EnterMyName macro, the variables document and dispatcher are defined as the type
object. Other common variable types include string, integer, and date. A third variable, named
args1, is an array of property values. A variable of type array allows a single variable to contain
multiple values, similar to storing multiple pages in a single book. Values in an array are usually
numbered starting from zero. The number in the parentheses indicates the highest usable number
to access a storage location. In this example, there is only one value, and it is numbered zero.
Explaining macro code
The following is an explanation of the code used in the EnterMyName macro. You may not
understand all the details, but the explanation of each line of code may give you some idea of how
a macro works.
sub EnterMyName
Defines the start of the macro
dim document as object
Defined as a variable
dim dispatcher as object
Defined as a variable
document = ThisComponent.CurrentController.Frame
ThisComponent refers to the current document.
CurrentController is a property referring to a service that controls the document. For example,
when you type, it is the current controller that takes note of what you type. CurrentController
then dispatches the changes to the document frame.
Frame is a controller property that returns the main frame for a document. Therefore, the
variable named document refers to a document’s frame, which receives dispatched commands.
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
Most tasks in LibreOffice are accomplished by dispatching a command. LibreOffice includes a
dispatch helper service, which does most of the work when using dispatches in macros. The
method CreateUnoService accepts the name of a service and it tries to create an instance of
that service. On completion, the dispatcher variable contains a reference to a DispatchHelper.
dim args1(0) as new com.sun.star.beans.PropertyValue
Declares an array of properties. Each property has a name and a value. In other words, it is a
name/value pair. The created array has one property at index zero.
args1(0).Name = "Text"
args1(0).Value = "Your name"
Gives the property the name “Text” and the value “Your name”, which is the text that is inserted
when the macro is run.
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
Chapter 13 Getting Started with Macros | 341