Jump to content

OpenOffice Basic

From Wikipedia, the free encyclopedia

Template:Short description Template:No footnotes OpenOffice Basic (formerly known as StarOffice Basic or StarBasic or OOoBasic) is a dialect of the programming language BASIC that originated with the StarOffice office suite and spread through OpenOffice.org and derivatives such as Apache OpenOffice and LibreOffice (where it is known as LibreOffice Basic). The language is a domain-specific programming language which specifically serves the OpenOffice application suite.

Example

Although OpenOffice Basic is similar to other dialects of BASIC, such as Microsoft's Visual Basic for Applications (VBA), the application programming interface (API) is very different, as the example below of a macro illustrates. While there is a much easier way to obtain the "paragraph count" document property, the example shows the fundamental methods for accessing each paragraph in a text document, sequentially.

<syntaxhighlight lang="vbnet"> Sub ParaCount ' ' Count number of paragraphs in a text document '

   Dim Doc As Object, Enum As Object, TextEl As Object, Count As Long
   Doc = ThisComponent

' Is this a text document?

   If Not Doc.SupportsService("com.sun.star.text.TextDocument") Then
       MsgBox "This macro must be run from a text document", 64, "Error"
       Exit Sub
   End If
   Count = 0

' Examine each component - paragraph or table?

   Enum = Doc.Text.CreateEnumeration
   While Enum.HasMoreElements
       TextEl = Enum.NextElement

' Is the component a paragraph?

       If TextEl.SupportsService("com.sun.star.text.Paragraph") Then
           Count = Count + 1
       End If
   Wend

'Display result

   MsgBox Count, 0, "Paragraph Count"

End Sub </syntaxhighlight>

VBA compatibility

Partial support for VBA API under <syntaxhighlight lang="vbnet" inline>Option VBASupport 1</syntaxhighlight> was introduced with OpenOffice 2.x circa 2005.[1] An underlying feature is <syntaxhighlight lang="vbnet" inline>Option Compatible</syntaxhighlight>, which enables a number of language features found in VBA (as well as other extensions beyond the StarBasic original).[2]

These features has been inherited into later versions as well as LibreOffice. LibreOffice has further built upon <syntaxhighlight lang="vbnet" inline>Option Compatible</syntaxhighlight> to provide more compatibility features and new features.[3][4] This option is implicitly enabled when opening a Microsoft Office document containing VBA code.[5]

The following Option directives are found in OpenOffice Basic and its dialects:[6]

Name Values Description StarOffice OpenOffice LibreOffice
Base 0 or 1 Sets whether arrays are 0- or 1-based. Does not affect the number of elements in an array like VBA, unless <syntaxhighlight lang="vbnet" inline>Option Compatible</syntaxhighlight> is also set. Template:Unknown Template:Yes Template:Yes
Compatible None Language extensions: accented characters as identifiers (O/L), VBA predefined constants (L), omission of New in Dim statements (L), default values for optional parameters (L), library preloading (L), Base affects array declaration (O/L) Template:Unknown Template:Yes Template:Yes
ClassModule None Turns the module into a class module, one that contains the members, properties, procedures and functions for a class. Depends on Compatible, either set explicitly or implied by VBASupport. Template:No Template:No Template:Yes
Explicit None Disallow implicit declaration of variables. Template:Unknown Template:Yes Template:Yes
Private None Disallow access from outside the library. Template:Unknown Template:Unknown Template:Yes
VBASupport 0 or 1 API compatibility with VBA. Implies <syntaxhighlight lang="vbnet" inline>Option Compatible</syntaxhighlight>. Template:No Template:Yes Template:Yes

See also

Further reading

|CitationClass=web }}

References

Template:Reflist

Template:OpenOffice Template:BASIC

  1. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  2. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  3. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  4. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  5. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
  6. ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}