22.01.2015 Views

OpenOffice.org Macros Explained - LibreOffice-NA.US

OpenOffice.org Macros Explained - LibreOffice-NA.US

OpenOffice.org Macros Explained - LibreOffice-NA.US

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

precede Option Explicit because they are not runnable. Although it would be even better if this became a<br />

compile-time error, OOo Basic does not resolve all variables and routines until run time.<br />

Listing 6. Use Option Explicit before the first runnable line in a macro.<br />

REM ***** BASIC *****<br />

Option Explicit<br />

TIP<br />

Use “Option Explicit” at the top of every macro that you write; it will save you a lot of time searching for<br />

errors in your code. When I am asked to debug a macro, the first thing I do is add “Option Explicit” at the<br />

top of each module.<br />

You can declare a variable with or without a type. A variable without an explicit type becomes a Variant,<br />

which is able to take on any type. This means that you can use a Variant to hold a numeric value and then, in<br />

the next line of code, overwrite the number with text. Table 4 shows the variable types supported by OOo<br />

Basic, the value that each type of variable contains immediately after declaration (“initial value”), and the<br />

number of bytes that each type uses. In OOo Basic, a variable's type can be declared by appending a special<br />

character to the end of the name when it is declared. The Post column in Table 4 contains the supported<br />

characters that can be post-fixed to a variable's name when the variable is declared.<br />

Table 4. Supported variable types and their initial values.<br />

Type Post Initial Bytes Convert Description<br />

Boolean False 1 CBool True or False<br />

Currency @ 0.0000 8 CCur Currency with 4 decimal places<br />

Date 00:00:00 8 CDate Dates and times<br />

Double # 0.0 8 CDbl Decimal numbers in the range of<br />

+/-1.79769313486232 x 10E308<br />

Integer % 0 2 CInt Integer from -32,768 through 32,767<br />

Long & 0 4 CLng Integer from -2147483648 through<br />

2147483647<br />

Object Null varies Object<br />

Single ! 0.0 4 CSng Decimal numbers in the range of<br />

+/-3.402823 x 10E38<br />

String $ "" varies CStr Text with up to 65536 characters<br />

Variant Empty varies CVar May contain any data type<br />

Although OOo Basic supports a Byte variable type, you can’t directly declare and use one. The function<br />

CByte, as discussed later, returns a Byte value that may be assigned to a variable of type Variant. With OOo<br />

version 2.0, you can declare a variable of type Byte, but the variable is assumed to be an externally defined<br />

object of type Byte rather than an internally defined Byte variable.<br />

Use the DIM keyword to explicitly declare a variable before use (see Table 5). You can declare multiple<br />

variables on a single line, and you can give each variable a type when it is declared. Variables with no<br />

declared type default to type Variant.<br />

32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!