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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Table 6. Examples of supported Def statements in <strong>OpenOffice</strong>.<strong>org</strong>.<br />

Def Statement<br />

DefBool b<br />

DefDate t<br />

DebDbl d<br />

DefInt i<br />

DefLng l<br />

DefObj o<br />

DefVar v<br />

Type<br />

Boolean<br />

Date<br />

Double<br />

Integer<br />

Long<br />

Object<br />

Variant<br />

3.2.3. Assigning values to variables<br />

The purpose of a variable is to hold values. To assign a value to a variable, type the name of the variable,<br />

optional spaces, an equals sign, optional spaces, and the value to assign to the variable, like so:<br />

X = 3.141592654<br />

y = 6<br />

The optional keyword Let may precede the variable name but serves no purpose other than readability. A<br />

similar optional keyword, Set, meant for Object variables, serves no purpose other than readability. These<br />

keywords are rarely used.<br />

3.2.4. Boolean variables are True or False<br />

Boolean variables have two valid values: True or False. They are internally represented by the Integer values<br />

-1 and 0, respectively. Any numeric value assigned to a Boolean that does not precisely evaluate to 0 is<br />

converted to True. The macro in Listing 8 introduces a few new concepts. A string variable, s, accumulates<br />

the results of the calculations, which are displayed in a dialog (see Figure 18). Adding CHR$(10) to the<br />

string causes a new line to be printed in the dialog. Unfortunately, accumulating the results in a string<br />

provides a more complicated macro than using simple statements such as “Print CBool(5=3)”, but the<br />

results are easier to understand (see Figure 18). In the OOo version of the document, a button is frequently<br />

available that allows you to run the macro immediately.<br />

Listing 8. Demonstrate conversion to type boolean.<br />

Sub ExampleBooleanType<br />

Dim b as Boolean<br />

Dim s as String<br />

b = True<br />

b = False<br />

b = (5 = 3) REM Set to False<br />

s = "(5 = 3) => " & b<br />

b = (5 < 7) REM Set to True<br />

s = s & CHR$(10) & "(5 < 7) => " & b<br />

b = 7 REM Set to True because 7 is not 0<br />

s = s & CHR$(10) & "(7) => " & b<br />

MsgBox s<br />

End Sub<br />

34

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

Saved successfully!

Ooh no, something went wrong!