The OpenOffice.org macro language is similar to the one in Microsoft Office because they are both based on BASIC. Both macro languages access the underlying implementation structures, which differ significantly and are therefore incompatible. This chapter emphasizes the portions of the language that do not access the underlying implementation.
This chapter shows how to assemble different components to produce an OOo macro that will compile and run. In a word: syntax. Correct syntax does not imply that the macro does what you want, only that the pieces are put together in a correct way. The phrases "Can I drive?" and "May I drive?" are both syntactically correct. The first phrase is about ability and the second phrase is about permission. In speech, these two questions may be understood to have the same meaning. The computer, on the other hand, does exactly what you ask, rather than what you mean.
The primary components that syntactically constitute an OpenOffice.org macro are statements, variables , subroutines and functions, and flow-control constructs. A statement in a macro-sometimes called a line-is a small, runnable portion of code that is usually written as a single line of text. Variables are containers that hold values that can change during execution of a macro. Subroutines and functions separate a macro into functional named groups of statements, allowing for better organization. Flow control directs which statements run and how many times.
OpenOffice.org runs one line of a macro at a time. Each line of a macro is delimited exactly the way it sounds- by a new line (see Listing 1 ).
Print "This is line one" Print "This is line two"
Lines that are too long may use more than one line by appending an underscore (_) to the end of the line (see Listing 2 ). The underscore must be the last character on the line for it to act as a line-continuation character. The underscore has no special meaning when it isn't the last character of a line, allowing it to be used inside strings and in variable names . When used as a continuation character, spaces may precede the underscore and are in some cases required to separate the underscore from what precedes it. For example, splitting the line "a+b+c" after the b requires a space between the b and the underscore, or the underscore is considered part of the variable name . Spaces that inadvertently follow a continuation character may cause a compile-time error. Unfortunately, the error is not that something follows the underscore, but that the next line is invalid.
Print "Strings are concatenated together " & _ "with the ampersand character"
You can place multiple statements on a single line by separating them with colons (see Listing 3 ). This is usually done for improved code readability. Each of the combined statements act as a single line of code while debugging the macro in the Integrated Development Environment (IDE). Listing 3, therefore, acts like three separate statements while using the IDE to single-step through the macro.
x = 0 : y = 0 : z = 0
Note | Macros are generically called "code." A programmer writes code, and OpenOffice.org runs one line of code at a time. |
You should liberally add remarks, which are also called comments, to all of the macros that you write. While writing a macro, remember that what is clear today may not be clear tomorrow, as time passes and new projects arise and memory fades all too quickly. The single quotation character and the keyword REM both indicate the start of a comment. All text on the same line following a comment indicator is ignored (see Listing 4 ). Comments are not considered runnable statements; they are ignored while single-stepping a macro.
REM Comments may start with the keyword REM. ReM It is not case-sensitive so this is also a comment. ' All text following the start of the comment is ignored X = 0 ' A comment may also start with a ' single quote z = 0 REM All text following the start of the comment is ignored
Note | Keywords, variables, and routine names in OOo Basic are not case-sensitive. Therefore, REM, Rem, and rEm all start a comment. |
Nothing can follow a line-continuation character, and this includes comments. All text following a comment indicator is ignored-even the continuation character. The logical result from these two rules is that a line-continuation character can never occur on the same line as a comment.
Warning |
For i = LBound(b()) To _ Rem hello UBound(b()) Print i Next