Operation Manual

2) Select your new macro EnterMyName and click Edit to open the macro in the Basic IDE.
The macro EnterMyName is shown in Listing 3.
The EnterMyName macro is not as complicated as it first appears. Learning a few things helps
significantly in understanding macros. The discussion starts with features near the top of the macro
listing and describes them.
Listing 3: Generated “EnterMyname” macro
REM ***** BASIC *****
Sub Main
End Sub
sub EnterMyName
rem -------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem -------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem -------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Text"
args1(0).Value = "Your name"
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
end sub
REM comments
All comments in macro coding begin with REM, which stands for remark. All text after REM and on
the same line is ignored. As a short cut, the single quote character (') can also be used to start a
comment.
LibreOffice Basic is not case-sensitive for keywords, so REM, Rem, and rem can all start a
comment. If you use symbolic constants defined by the Application Programming Interface (API), it
is safer to assume that the names are case-sensitive. Symbolic constants are an advanced topic
not covered by this user guide and are not required when using the macro recorder in LibreOffice.
Defining subroutines with SUB
Individual macros are stored in subroutines and these subroutines begin with the keyword SUB.
The end of a subroutine is indicated by the words END SUB. The code starts by defining the
subroutine named Main, which is empty and does nothing. The next subroutine, EnterMyName,
contains the generated code for your macro.
Note
LibreOffice always creates an empty subroutine named Main when it creates a
module.
There are advanced topics that are beyond the scope of this user guide, but knowing about them
might be of interest:
You can write a macro so that values can be passed to the subroutine. The values are
called arguments. However, recorded macros in LibreOffice do not accept arguments.
340 | Getting Started with LibreOffice 4.0