Macros

Macros record a series of user actions and replay them as needed.

You can view/edit macros at: Tools menu > Macros > Organize Macros > OpenOffice Basic

A macros can contain multiple procedures.
Running the macros will run "Sub Main".

Macros can be run
- Manually: Tools menu > Macros > Run Macro
- Toolbar Button Click: Tools menu > Customize > Customize Toolbar > ...
- Keyboard Shortcut: Tools menu > Customize > Keyboard Tab > ...

Event Handler

A macro can be called when an event occurs. This macros is called an event handler.

1) Write your macro

2) Tools menu > Customize > Events Tab
- events can be saved to OpenOffice Calc, or to a specific file
Programming

Functions

Custom user functions determine the contents of a cell.

Function written in Basic:

Function Cube( cellA ) As Integer
    Cube = cellA * cellA * cellA
End Function

Function SimpleSum( cellA, cellB ) As Decimal
    SimpleSum = cellA + cellB
End Function

Cell contents:

=Cube(A1)
=SimpleSum(A1;A2)

Functions provide a return value by assigning to a variable with the same name as the function.
Functions will continuing executing after their "return" variable has been assigned to, and that value can be overwritten.
Functions return 0 if nothing is assigned.

Procedures

aka Sub
aka User Routine

Procedures can be run from any point in the program.
Procedures have no return value.

Procedure written in Basic:

Sub Test
    ' ... here is the actual code of the procedure
End Sub

Syntax

Comments

' these are comments

Parameters

' Default is pass by reference
Sub Test (A As Integer, B As String)
End Sub

' Specify pass by value
Sub Test (ByVal A As Integer)
End Sub

' Optional parameters
Sub Test (Optional A As Integer)
End Sub

Return type

Function Cube( cellA ) As Integer
    Cube = cellA * cellA * cellA
End Function

Declaration

Dim ErrorOccured As Boolean

If

If MyVar Then
    ' code
End If

If MyVar Then
    ' code
Else
    ' code
End If

Logic

If Not MyVar Then
    ' code
End If

If A OR B Then
    ' code
End If

Exit function

Exit Function

Exit procedure

Exit Sub

Snippets

Set focus on a particular cell

Sheet = ThisComponent.Sheets.getByName("Sheet1")
Cell = Sheet.getCellRangeByName("B3")
ThisComponent.CurrentController.select(Cell)