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.

3.2.9. Declare variables with special types<br />

You can use the keywords “As New” to define a variable as a known UNO struct. The word “struct” is an<br />

abbreviated form of the word “structure” that is frequently used by computer programmers. A struct has one<br />

or more data members, each of which may have different types. Structs are used to group associated data<br />

together.<br />

Option Compatible provides a new syntax to define variables of known and unknown types. A simple<br />

example is declaring a variable of a specific type even if the type is not known to OOo Basic.<br />

Option Compatible 'Supported in OOo 2.0<br />

Sub Main<br />

Dim oVar1 As Object<br />

Dim oVar2 As MyType<br />

Set oVar1 = New MyType 'Supported in OOo 2.0<br />

Set oVar2 = New MyType 'Supported in OOo 2.0<br />

Set oVar2 = New YourType 'Error, declared as MyType not YourType.<br />

A new OLE object factory was introduced with OOo 2.0, which allows new types to be created. The new<br />

functionality allows OOo Basic to manipulate Microsoft Word documents on Microsoft Windows operating<br />

systems if Microsoft Office is also installed on the computer.<br />

Sub Main<br />

Dim W As Word.Application<br />

Set W = New Word.Application<br />

REM Dim W As New Word.Application 'This works in OOo 2.0<br />

REM W = CreateObject("Word.Application") 'This works in OOo 2.0<br />

W.Visible = True<br />

End Sub<br />

The use of CreateObject() does not rely on “Option Compatible” because this functionality is provided by<br />

the new OLE object factory released with OOo 2.0.<br />

3.2.10. Object variables<br />

An Object is a complex data type that can contain more than a single piece of data. The code in Listing 16<br />

shows an example of a complex data type. In <strong>OpenOffice</strong>.<strong>org</strong>, Object variables are intended to hold complex<br />

data types created and defined by OOo Basic. When a variable of type Object is first declared, it contains the<br />

special value Null, which indicates that no valid value is present.<br />

Use the Variant type, not Object, to refer to <strong>OpenOffice</strong>.<strong>org</strong> internals. This is discussed later.<br />

3.2.11. Variant variables<br />

Variant variables are able to hold any data type. They take on the type of whatever is assigned to them. When<br />

a variable of type Variant is first declared, it contains the special value Empty, which indicates that no value<br />

has been assigned to the variable. A Variant may be assigned an integer in one statement and then assigned<br />

text in the next. No typecasting occurs when assigning data to a Variant; it simply becomes the appropriate<br />

type.<br />

The chameleon-like behavior of Variant variables allows them to be used as any other variable type.<br />

However, this flexibility comes with a cost: time. One final problem is that it isn’t always obvious what type<br />

a Variant will become after making some assignments.<br />

42

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

Saved successfully!

Ooh no, something went wrong!