Libreofice Basic: Libreoffe Refcard
Libreofice Basic: Libreoffe Refcard
LibreOffe RefCard
Type name Description Initialized to
LibreOfice Basic Object
Single
Objects. Allow to manipulate LibreOffice API objects.
Floating numbers (32 bits).
Null
0.0
LibOBasic-2-Overview-Flat-Letter-EN-v110.odt
Function Calling A Command Associated With A LibreOffice Menu
Executes an action and returns a value. 101
Naming hint: verb at the indicative: IsXxx(), etc.
Use the Dispatcher, and pass it the wanted UNO menu command.
Declaration Function FuncName(parameters) As SomeType
Function FuncName(parameters) As SomeType Knowing UNO Menus Commands
Structure
'instructions UNO menu commands: see the menubar.xml files in the LibreOffice installation directory
'somewhere, define the return value: (OS specific), under share/config/soffice.cfg/modules. Subdir menubar of the
FuncName = SomeValue
End Function wanted module (eg: sglobal/menubar/menubar.xml, etc.).
Use SomeVar = FuncName(arguments) All commands start with .uno:
If no argument: SomeVar = FuncName() Ex : ".uno:Open" (File > Open), ".uno:OptionsTreeDialog" (Tools > Options), etc.
☞ A Function may be called like a Sub (without caring of the return value). Program Skeleton
Parameters Dim Frame As Variant
Parameter : a value the subprogram declaration specifies. Dim Dispatch As Object
Argument : the actual value the caller passes to the subprogram. Dim Args() As Variant 'contents depends from context
Dim UnoCmd As String
Ex : MySub(ByRef AParam as Long, ByVal OtherParam As String, _ Frame = ThisComponent.CurrentController.Frame
Optional ByRef SomeParam As String) UnoCmd = 'UNO command to run (above)
ByRef By reference (default). The parameter points to the argument passed by the Dispatch = createUnoService("com.sun.star.frame.DispatchHelper")
Dispatch.executeDispatch(Frame, UnoCmd, "", 0, Args())
caller.
☞ Any modification of a ByRef item is propagated to the caller at return time. where UnoCmd is the command found in the files above.
ByVal By value. The parameter is a copy of the argument passed by the caller. Exemples
☞ Value modifications are local to the called and not propagated to the caller. (only modified parts are shown)
Optional Optional parameter.
Ex1. Calling Print Preview
Giving a default value to an optional parameter:
If IsMissing(SomeParam) Then SomeParam = SomeValue Dispatch.executeDispatch(Frame, ".uno:PrintPreview", "", 0, Args())
☞ The identifier is always available in the subprogram.
Ex2. Showing/Hiding The Sidebar
Control Structures Dim Args(0) As New com.sun.star.beans.PropertyValue
Loops Args(0).Name = "Sidebar"
Args(0).Value = True 'or False depending on aim
Repeat a sequence of instructions. Dispatch.executeDispatch(Frame, ".uno:Sidebar", "", 0, Args())
☞ Premature exit possible using Exit For or Exit Do according to situation.
For … Next Error Management
For each counter value … You must know the counter bounds. In Basic, error management is available using:
For i = Start To End [Step By default, increment Step is 1. • On Error Xxx : instructions for error interception;
Increment]
'instructions ☞ Counters are often named as i, j, k, etc. • Err, Erl and Error : functions to get information about the last error met.
Next Never set the counter in the loop instructions! Error Information Functions
For Each … Next Err The error code.
For each item … The number of items is unknown. ☞ An error code of 0 (zero) means “no error”.
For Each item In SomeObject item must be of a compatible type. Use If Err Then … to check error presence.
'do smthg with item Error
Next The message that describes the error.
Erl The line number where the error occurred.
Do While … Loop
☞ You may create custom errors by setting a value to Err :
Do While Condition Condition is evaluated first. Err = 1234 generates error 1234.
'instructions Infinite loops (Condition never met)!
Loop On Error – Intercepting Errors
or… Error interception is active as long as it has not been canceled.
While Condition ☞ Older syntax, for compatibility only. Doesn’t sup- On Error Goto MyLabel Activates error interception. If an error occurs, the
'instructions port Exit.
Wend execution continues to MyLabel.
Do not use!
☞ In the program body, you must define the label
Do Loop ... Until MyLabel: (beware to the semicolon character).
Do Condition is evaluated last. On Error Resume Next Activates error interception. If an error occurs, the
'instructions Infinite loops (Condition never met)! execution continues to the next instruction.
Loop Until Condition On Error Goto 0 Cancels error interception.
Conditional Tests ☞ In a Sub or Function, you might prefer the On Local Error Xxx syntax. This doesn’t
A branch that allows to take action according to a given situation. requires calling On Error Goto 0 to cancel error interception: canceling is automati-
If (alone) cally performed when leaving the Sub or Function.
☞ On Local Error Goto Xxx has precedence on any preexisting On Error Goto
If Condition Then SomeInstruction
Xxx.
If Then Else
If Condition Then Different Ways Of Running A Macro
'InstructionsThen ▼ Method LibreOffice Document Type Current Document
Else
'InstructionsElse Using a toolbar button ● ●
End If Using a menu ● ●
If ElseIf Using a shortcut ● ●
If Condition Then Instead of nested Ifs. Through an event ● ●
'InstructionsThen1
ElseIf OtherCondition Then
'InstructionsThen2
Else
'InstructionsElse
End If Credits
Select Author : Jean-François Nifenecker – [email protected]
Select Case SomeVariable Choose among several possibilities, We are like dwarves perched on the shoulders of giants, and thus we are able to see more and farther than the
Case Value1, Value2 according to SomeVariable actual latter. And this is not at all because of the acuteness of our sight or the stature of our body, but because we are
'instructions for SomeValue carried aloft and elevated by the magnitude of the giants (Bernard de Chartres [attr.])
Case Value3 To Value4 value.
History
'instructions for OtherValue
Case Else Version Date Comments
'instructions for other situations
End Select 1.10 02/11/2018 First EN version
Loading A Code Library
For readability and maintainability, organize your code in several libraries (RefCard #1).
☞ The Standard code library is the only loaded library at document opening. Others must
be explicitly loaded to gain access to their code.
Library names are case sensitive!
Loading From The Local Container (document)
Checking existence LibExists = BasicLibraries.hasByName("MyLib")
Loading BasicLibraries.loadLibrary("MyLib")
License
Loading From A Global Container This refcard is placed under the
Same as above but BasicLibraries is replaced with GlobalScope.BasicLibraries. CreativeCommons BY-SA v4 (intl) license.
Mind to identifiers collisions between libraries! You may qualify names using: More information:
container.library.module.name (all or part). https://creativecommons.org/licenses/by-sa/4.0/
Ex: GlobalScope.Tools.Strings.ClearMultiDimArray(MyArray, 3)