Mastering LibreOffice Basic: Essential Examples & Tutorials

Charles Jul 17, 2026

LibreOffice Basic is a fully-featured scripting language bundled with LibreOffice, offering users the power to automate tasks and extend functionality. If you're new to scripting or just looking to enhance your LibreOffice skills, let's explore some practical examples of LibreOffice Basic to get you started.

LibreOffice Writer: The Ultimate Keyboard Shortcuts Cheat Sheet
LibreOffice Writer: The Ultimate Keyboard Shortcuts Cheat Sheet

LibreOffice Basic shares similarities with other BASIC languages, making it accessible to both beginners and experienced programmers. It's akin to macros in popular suites like Microsoft Office, yet it brings more extensive capabilities through its integration with LibreOffice components. Now, let's dive into some examples.

LibreOffice 3.5 Released - OMG! Ubuntu
LibreOffice 3.5 Released - OMG! Ubuntu

Hello World & Simple Calculations

Begin your LibreOffice Basic journey with a classic: the "Hello, World!" example. This will help you understand the basics of creating a simple script and running it.

the employee edit form is displayed in this screenshote screengrafion for microsoft
the employee edit form is displayed in this screenshote screengrafion for microsoft

Next, let's perform a simple calculation. LibreOffice Basic allows you to create scripts that automate repetitive tasks, saving you time and effort. For instance, you can create a script that calculates the sum of a range of cells in a spreadsheet.

Hello World Example

an open office / libre office the database module help and advice, graded materials from getting started to intermediae level work
an open office / libre office the database module help and advice, graded materials from getting started to intermediae level work

Here's a simple "Hello, World!" example in LibreOffice Basic:

```basic Sub HelloWorld MsgBox "Hello, World!" End Sub ```

Save this script as a macro and run it. A message box will appear displaying the message 'Hello, World!'

Simple Calculation Example

You Can Now Use LibreOffice in Your Browser: Here's How
You Can Now Use LibreOffice in Your Browser: Here's How

Now, let's calculate the sum of cells A1 to A10 in a spreadsheet. Assume that these cells contain numbers 1 through 10. Here's the script:

```basic Sub CalculateSum Dim total As Single For i = 1 To 10 total = total + ThisComponent.Sheets.getByName("Sheet1").getCellRangeByName("A" & i).getValue() Next i MsgBox "The sum is: " & total End Sub ```

Working with Ranges & Conditional Formatting

LibreOffice Basic allows you to interact with spreadsheets in more advanced ways than simple calculations. You can work with ranges of cells, apply conditional formatting, and much more.

LibreOffice Calc Tutorial - [ Spreadsheet Tutorial ]
LibreOffice Calc Tutorial - [ Spreadsheet Tutorial ]

In this section, we'll explore examples of working with ranges and applying conditional formatting to highlight cells based on their values.

Working with Ranges

Libre Office: Open Source Word Processing
Libre Office: Open Source Word Processing
the powerpoint all shortcuts list is shown
the powerpoint all shortcuts list is shown
the top 10 excel functions for beginners to use in your workbook or notebook
the top 10 excel functions for beginners to use in your workbook or notebook
the word libreoffice is displayed in an open source window, and it appears to have been modified
the word libreoffice is displayed in an open source window, and it appears to have been modified
What is LibreOffice |Libre Office General Features and installation
What is LibreOffice |Libre Office General Features and installation
Microsoft Office shortcut
Microsoft Office shortcut
File extension in computer system
File extension in computer system
a computer screen with the text getting started with liberoffe base
a computer screen with the text getting started with liberoffe base
How to set the default font in LibreOffice Calc?
How to set the default font in LibreOffice Calc?

Here's an example that demonstrates how to iterate through a range of cells and perform an operation on each cell:

```basic Sub WorkWithRanges Dim myRange As org.libreoffice.script.Variant myRange = ThisComponent.Sheets.getByName("Sheet1").getCellRangeByName("A1:E10") For i = 0 To myRange.getRows() For j = 0 To myRange.getColumns() myRange.getCellByPosition(j, i).setValue(myRange.getCellByPosition(j, i).getValue() * 2) Next j Next i End Sub ```

This script takes each value in the range A1:E10 and multiplies it by 2.

Conditional Formatting

Now, let's apply conditional formatting to highlight cells based on their values. In this example, we'll highlight cells in column A if their value is greater than 50:

```basic Sub ApplyConditionalFormatting Dim myRange As org.libreoffice.script.Variant myRange = ThisComponent.Sheets.getByName("Sheet1").getCellRangeByName("A1:A10") myRange.setStyle(7) ' Green fill color myRange.setString daylight as " Hits" ' Font color for hits myRange.setPropertyValue("Border") = 1 ' Set border to 1 myRange.setPropertyValue("BorderColor") = RGB(0, 0, 0) ' Set border color to black myRange.setPropertyValue("ConditionalFormat") = True ' Enable conditional formatting myRange.setPropertyValue("CondFormat1") = "FORMATTED_VALUE" ' Use formatted value as condition myRange.setPropertyValue("CondFormat2") = 50 ' Set comparison value to 50 myRange.setPropertyValue("CondFormat3") = 0 ' Use 0 as operator myRange.setPropertyValue("CondFormat4") = True ' Enable the rule End Sub ```

Automating Tasks with User Interaction

LibreOffice Basic scripts can interact with users through dialogs and controls. In this section, we'll create examples that ask the user for input and perform tasks based on their responses.

First, we'll create a simple input box that takes user input and displays it in a message box. Then, we'll create a dialog with multiple controls that calculates and displays the factorial of a number entered by the user.

User Input Example

Here's an example that takes user input, stores it in a variable, and displays it in a message box:

```basic Sub TakeUserInput Dim userInput As String userInput = InputBox("Please enter something:", "User Input") MsgBox "You entered: " & userInput End Sub ```

Factorial Calculator Dialog

In this example, we'll create a dialog with an input field, a label, and a calculate button. When the user clicks the button, the script will calculate the factorial of the entered number and display the result in a message box. Save this as a macro and run it:

```basic Option Dialogs Sub FactorialCalculator Dim dialog As XDialog Dim numberInput As XTextField Dim calculateButton As XButton dialog = ThisComponent.Sheets.createInstance("Dialogs garnison.TEXTFIELD") numberInput = dialog.addTextField(1, 20, 10) Dialog.addCommand("Calculate", " calculatesFactorial", "Calculate") ' Button label, event handler, and button ID Dialog.addCommand("Cancel", "", "-1") Function calculatesFactorial() Dim number As Integer number = numberInput.getText() If number = "" Then MsgBox "Please enter a valid number." Exit Function End If Dim factorial As Integer factorial = 1 For i = 1 To number factorial = factorial * i Next i MsgBox "Factorial of " & number & " is: " & factorial End Function dialog.Show() End Sub ```

With these examples, you've seen the power of LibreOffice Basic in automating tasks, working with spreadsheets, and interacting with users. The possibilities are vast, limited only by your creativity and imagination. So, keep exploring and expanding your LibreOffice Basic skills!