Getting Started Guide
Pulling the macro together
The following details are very complete; it is not important to understand all of the
details. The first line defines the start of the macro.
sub EnterMyName
Declare two variables:
dim document as object
dim dispatcher as object
ThisComponent refers to the current document.
The CurrentController property of a document refers to a service that “controls” the
document. For example, when you type, it is the current controller that notices. The
current controller then dispatches the changes to the document’s frame.
The Frame property of a controller returns a main frame for a document. Therefore,
the variable named document refers to a document’s frame, which receives
dispatched commands.
document = ThisComponent.CurrentController.Frame
Most tasks in OpenOffice.org are accomplished by dispatching a command. OOo
includes a dispatch helper service, which does most of the work to use 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.
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
Declare 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.
dim args1(0) as new com.sun.star.beans.PropertyValue
Give the property the name “Text” and the value “Andrew Pitonyak”, which is the text
that is inserted when the macro is run.
args1(0).Name = "Text"
args1(0).Value = "Andrew Pitonyak"
This is where the magic happens. The dispatch helper sends a dispatch to the
document’s 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.
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
Finally, the end of the subroutine.
end sub
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 such that the last command leaves the cursor ready
for the next command?
328 Getting Started with OpenOffice.org 3.3










