Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions




OpenOffice 3.x Getting Started Guide
Previous Page Home Next Page

Viewing and editing the macro

You can view and edit the macro that was just created. Use Tools > Macros > Organize Macros > Basic to open the Basic Macros dialog (see Figure 3). Select the new macro and click Edit to open the macro in the Basic IDE (Integrated Development Environment).

Listing 1: 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("")
 rem ---------------------------------------------------------------
 dim args1(0) as new
 args1(0).Name = "Text"
 args1(0).Value = "Andrew Pitonyak"
 dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
 end sub

The macro in Listing 1 is not as complicated as it first appears. Learning a few things helps significantly in understanding the generated macros. The discussion starts with features near the top of the macro listing and describes them. If you like to avoid details, then simply change the text “Andrew Pitonyak" to what you want to insert at the current cursor position.

Comments start with REM

The keyword REM, short for remark, starts a macro comment. All text after REM (on the same line) is ignored. As a short cut, the single quote character can also be used to start a comment.

Image:Tip.pngStarBasic is not case-sensitive for keywords, so REM, Rem, and rem all start a comment. If you use symbolic constants defined by the API, it is safer to assume that the names are case-sensitive—symbolic constants are an advanced topic not usually needed by people who use the macro recorder.

Defining subroutines with SUB

Individual macros are stored in subroutines defined 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. creates an empty subroutine named Main when it creates a module.

There are advanced topics that are beyond the scope of this document, 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. Recorded macros do not accept arguments.
  • Another kind of subroutine is called a function. A function is a subroutine that returns a value. The keyword FUNCTION is used rather than SUB to define a function. Generated macros are always of type SUB.

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 statement is similar to setting aside a piece of paper to be used to store a message or note.

The EnterMyName macro defines the variables document and dispatcher as 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.

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 are accomplished by dispatching a command. OOo version 2.0 introduced the 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("")

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

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>/tt>. 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.

<tt>dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())

Finally, the end of the subroutine.

end sub

OpenOffice 3.x Getting Started Guide
Previous Page Home Next Page

  Published under the terms of the Creative Commons License Design by Interspire