21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Files and Directories<br />

operation of certain functions. The effect on any particular function is described with that function, below.<br />

As a statement, CompatibilityMode( value ) takes a Boolean value to set or clear the mode. As a function,<br />

CompatibilityMode() returns the Boolean value of the mode.<br />

CompatibilityMode( True ) 'set mode<br />

CompatibilityMode( False) 'clear mode<br />

Dim bMode as Boolean<br />

bMode = CompatibilityMode()<br />

Searching Through Directories<br />

The Dir function in <strong>OpenOffice</strong>.<strong>org</strong> Basic is responsible for searching through directories for files and subdirectories.<br />

When first requested, a string containing the path of the directories to be searched must be assigned to<br />

Dir as its first parameter. The second parameter of Dir specifies the file or directory to be searched for.<br />

<strong>OpenOffice</strong>.<strong>org</strong> Basic returns the name of the first directory entry found. To retrieve the next entry, the Dir<br />

function should be requested without parameters. If the Dir function finds no more entries, it returns an empty<br />

string.<br />

The following example shows how the Dir function can be used to request all files located in one directory. The<br />

procedure saves the individual file names in the AllFiles variable and then displays this in a message box.<br />

Sub ShowFiles<br />

Dim NextFile As String<br />

Dim AllFiles As String<br />

AllFiles = ""<br />

NextFile = Dir("C:\", 0)<br />

While NextFile ""<br />

AllFiles = AllFiles & Chr(13) & NextFile<br />

NextFile = Dir<br />

Wend<br />

MsgBox AllFiles<br />

End Sub<br />

The 0 (zero) used as the second parameter in the Dir function ensures that Dir only returns the names of files<br />

and directories are ignored. The following parameters can be specified here:<br />

0 : returns normal files<br />

16 : sub-directories<br />

The following example is virtually the same as the preceding example, but the Dir function transfers the value 16<br />

as a parameter, which returns the sub-directories of a folder rather than the file names.<br />

Sub ShowDirs<br />

Dim NextDir As String<br />

Dim AllDirs As String<br />

AllDirs = ""<br />

NextDir = Dir("C:\", 16)<br />

While NextDir ""<br />

AllDirs = AllDirs & Chr(13) & NextDir<br />

NextDir = Dir<br />

Wend<br />

MsgBox AllDirs<br />

End Sub<br />

Note – VBA : When requested in <strong>OpenOffice</strong>.<strong>org</strong> Basic, the Dir function, using the parameter 16, only returns<br />

the sub-directories of a folder. In VBA, the function also returns the names of the standard files so that further<br />

checking is needed to retrieve the directories only. When using the CompatibilityMode ( true ) function,<br />

<strong>OpenOffice</strong>.<strong>org</strong> Basic behaves like VBA and the Dir function, using parameter 16, returns sub-directories and<br />

standard files.<br />

Chapter 3 · Runtime Library 43

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

Saved successfully!

Ooh no, something went wrong!