Chapter 2: Language Constructs


Overview

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 ).

Listing 1: OpenOffice.org runs one line of a macro at a time.
start example
 Print "This is line one" Print "This is line two" 
end example
 

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.

Listing 2: Append an underscore to the end of a line to continue on the next line.
start example
 Print "Strings are concatenated together " & _       "with the ampersand character" 
end example
 

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.

Listing 3: Set the variables x, y, and z to zero.
start example
 x = 0 : y = 0 : z = 0 
end example
 
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.

Listing 4: You should add comments to all of the macros that you write.
start example
 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 
end example
 
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.

When anything follows a line-continuation character, the line is not continued . This may not cause an error. See Listing 5 .

Listing 5: OOo Basic does not generate a compilation error. Depending on the OOo version, a run-time error may be generated or the error may be ignored.
Warning  
start example
 For i = LBound(b()) To _ Rem hello   UBound(b())   Print i Next 
end example
 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net