LibreOffice-Examples
Jump to navigation
Jump to search
Getting Started Examples
Quickstart: Base
#include <MsgBoxConstants.au3>
#include <File.au3>
#include "LibreOfficeBase.au3"
; Create a New, visible, Blank Libre Office Document.
Global $oDoc = _LOBase_DocCreate(True, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Create a new Base Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set the Database type.
_LOBase_DocDatabaseType($oDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set Base Document Database type. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a unique file name
Global $sSavePath = _TempFile(@DesktopDir & "\", "BaseDocTestFile_", ".odb")
; Save The Base Document To Desktop Directory.
_LOBase_DocSaveAs($oDoc, $sSavePath)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to save the Base Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Database Object.
$oDBase = _LOBase_DatabaseGetObjByDoc($oDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Retrieve the Base Document Database Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Connect to the Database
$oConnection = _LOBase_DatabaseConnectionGet($oDBase)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to create a connection to the Database. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Add a Table to the Database.
_LOBase_TableAdd($oConnection, "tblNew_Table", "Col1")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to add a table to the Database. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Close the connection.
_LOBase_DatabaseConnectionClose($oConnection)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to close a connection to the Database. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Save the changes to the document.
_LOBase_DocSave($oDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Save the Base Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Close the Base Document.
_LOBase_DocClose($oDoc, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Close the Base Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
Quickstart: Calc
#include <MsgBoxConstants.au3>
#include <File.au3>
#include "LibreOfficeCalc.au3"
; Create a New, visible, Blank Libre Office Document.
Global $oDoc = _LOCalc_DocCreate(True, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Create a new Calc Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the presently active Sheet.
Global $oSheet = _LOCalc_SheetGetActive($oDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve the currently active Sheet Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B1.
Global $oCell = _LOCalc_RangeGetCellByName($oSheet, "B1")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve B1 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set Cell B1's Text.
_LOCalc_CellString($oCell, "Sales")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B1 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B2.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "B2")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve B2 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set B2 Cell Value to 50
_LOCalc_CellValue($oCell, 50)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B2 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B3.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "B3")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve B3 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set B3 Cell Value to 15.50
_LOCalc_CellValue($oCell, 15.50)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B3 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell A5.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "A5")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve A5 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set Cell A5's Text.
_LOCalc_CellString($oCell, "Total")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B1 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B5.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "B5")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve A5 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set the Formula for Cell B5
_LOCalc_CellFormula($oCell, "=SUM(B2:B3)")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set Cell B5 Formula. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a unique file name
Global $sSavePath = _TempFile(@DesktopDir & "\", "CalcDocTestFile_", ".ods")
; Save The Calc Document To Desktop Directory.
_LOCalc_DocSaveAs($oDoc, $sSavePath)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to save the Calc Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Close the Calc Document.
_LOCalc_DocClose($oDoc, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Close the Calc Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
Quickstart: Writer
#include <MsgBoxConstants.au3>
#include <File.au3>
#include "LibreOfficeWriter.au3"
; Create a New, visible, Blank Libre Office Document.
Global $oDoc = _LOWriter_DocCreate(True, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Create a new Writer Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the visible document cursor (caret) to insert text with.
Global $oViewCursor = _LOWriter_DocGetViewCursor($oDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve the View Cursor Object for the Writer Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Insert some text at the ViewCursor.
_LOWriter_DocInsertString($oDoc, $oViewCursor, "Hello from LibreOffice, and AutoIt!")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to insert text. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a unique file name
Global $sSavePath = _TempFile(@DesktopDir & "\", "DocTestFile_", ".odt")
; Save The Document To Desktop Directory.
_LOWriter_DocSaveAs($oDoc, $sSavePath)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to save the Writer Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Close the document.
_LOWriter_DocClose($oDoc, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to close opened L.O. Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
Starting Portable LibreOffice Example
#include <MsgBoxConstants.au3>
#include <File.au3>
#include "LibreOffice_Helper.au3"
#include "LibreOfficeCalc.au3"
#include "LibreOfficeWriter.au3"
; Update this path to match the path to your Portable LibreOffice/OpenOffice folder.
Global $sPathToPortable = "C:\Portable Apps\LibreOfficePortablePrevious"
; Initialize Portable LibreOffice
_LO_InitializePortable($sPathToPortable)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Initialize L.O. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a new Writer Document.
Global $oWriterDoc = _LOWriter_DocCreate(True, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Create a new Writer Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the visible document cursor (caret) to insert text with.
Global $oViewCursor = _LOWriter_DocGetViewCursor($oWriterDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve the View Cursor Object for the Writer Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Insert some text at the ViewCursor.
_LOWriter_DocInsertString($oWriterDoc, $oViewCursor, "Hello from LibreOffice, and AutoIt!")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to insert text. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a new Calc Document.
Global $oCalcDoc = _LOCalc_DocCreate(True, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Create a new Calc Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the presently active Sheet.
Global $oSheet = _LOCalc_SheetGetActive($oCalcDoc)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve the currently active Sheet Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B1.
Global $oCell = _LOCalc_RangeGetCellByName($oSheet, "B1")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve B1 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set Cell B1's Text.
_LOCalc_CellString($oCell, "Sales")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B1 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B2.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "B2")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve B2 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set B2 Cell Value to 50
_LOCalc_CellValue($oCell, 50)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B2 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B3.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "B3")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve B3 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set B3 Cell Value to 15.50
_LOCalc_CellValue($oCell, 15.50)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B3 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell A5.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "A5")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve A5 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set Cell A5's Text.
_LOCalc_CellString($oCell, "Total")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set B1 Cell content. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Retrieve the Object for Cell B5.
$oCell = _LOCalc_RangeGetCellByName($oSheet, "B5")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to retrieve A5 Cell Object. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Set the Formula for Cell B5
_LOCalc_CellFormula($oCell, "=SUM(B2:B3)")
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Set Cell B5 Formula. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a unique file name
Global $sSavePath = _TempFile(@DesktopDir & "\", "WriterDocTestFile_", ".odt")
; Save The Writer Document To Desktop Directory.
_LOWriter_DocSaveAs($oWriterDoc, $sSavePath)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to save the Writer Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Create a unique file name
$sSavePath = _TempFile(@DesktopDir & "\", "CalcDocTestFile_", ".ods")
; Save The Calc Document To Desktop Directory.
_LOCalc_DocSaveAs($oCalcDoc, $sSavePath)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to save the Calc Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Close the Writer document.
_LOWriter_DocClose($oWriterDoc, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to close opened L.O. Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)
; Close the Calc Document.
_LOCalc_DocClose($oCalcDoc, False)
If @error Then Exit MsgBox($MB_OK + $MB_ICONERROR + $MB_TOPMOST, "Error", "Failed to Close the Calc Document. Error:" & @error & " Extended:" & @extended & " On Line: " & @ScriptLineNumber)