Operation Manual

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())
This is where the magic happens. The dispatch helper sends a dispatch to the document
frame (stored in the variable named document) with the command .uno:InsertText. The next
two arguments, frame name and search flags, are beyond the scope of this document. The
last argument is the array of property values to be used while executing the command
InsertText.
end sub
The last line of the code ends the subroutine.
Creating a macro
When creating a macro, it is important to ask two questions before recording:
1) Can the task be written as a simple set of commands?
2) Can the steps be arranged so that the last command leaves the cursor ready for the next
command or entering text or data into the document?
A more complicated example of a macro
A common task is to copy rows and columns of data from a web site and format them as a table in
a text document as follows:
1) Copy the data from the web site to the clipboard.
2) To avoid strange formatting and fonts, paste the text into a Writer document as unformatted
text.
3) Reformat the text with tabs between columns so that it can be converted into a table using
Table > Convert > Text to Table on the main menu bar.
With the two questions given above in mind, inspect the text to see if a macro can be recorded to
format the text. An example of copied data showing the FontWeight constants group from the API
web site (Figure 336). The first column in this example indicates a constant name and each name
is followed by a space and a tab, and each line has two trailing spaces.
The first column in the table should contain a numeric value, the second column the name, and the
third column the description. This conversion is easily accomplished for every row except for
DONTKNOW and NORMAL, which do not contain a numeric value, but the values are between 0
and 100 and can be entered manually.
348 | Getting Started with LibreOffice 5.0