I am looking for a reference regarding the LibreOffice object model. I want to write a PowerShell script using the Impress object model to open and save presentations.
Object models are things like objects and classes according to wikipedia.
If that’s what your looking for, I guess this LO help page might help? On the right side in the “contents” section is also a lot of info regarding macros and scripting.
Hello, and thank you for responding. The provided reference is helpful, but does not appear to contain the information that I was looking for. When writing scripts/programs (e.g., PowerShell), many applications (e.g., Microsoft PowerPoint) expose some of their functionality by way of COM Objects. Does LibreOffice do something similar?
###########################################################################
This post is about the Libre-Office Object Model.
This post is a genuine Quick Start for ANYONE who would like to automate / program Libre Office using Libre-Office BASIC.
The first thing to know and understand are the “Objects” of Libre Office.
MS Office has an Object Browser which appears to be lacking in Libre Office, although, there actually is a way to browse the “Objects” of Libre Office.
When I say “browse” I mean being able to view the objects, look inside of them and see what’s available and how to access and manipulate the “Objects” for my purposes.
###########################################################################
What follows is an account of the simple steps I’ve used to learn the “Objects” of Libre Office to automate common tasks within Libre Writer using Libre-Office BASIC.
Below is a simple account of my actions; no warranties expressed or implied as to the fitness of my account or actions for any other human-being’s use or purposes.
###########################################################################
NEED
I needed to insert the creation date of a Libre-Writer document into a Libre-Writer document. It’s a very basic need and should NOT require me to become lost in the bowels of software engineering to accomplish the task!
NOTE
At the “document” level, Libre Calc uses the same general-object model that Writer uses for its “documents” so knowing the high-level Libre Writer document model is to generally know the high-level Libre Calc document object model.
###########################################################################
I launched Libre Writer
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Macro recording must be enabled
I enabled macro recording with the following menu navigation and selection,
Tools menu → Options (Alt+F12)
LibreOffice
Advanced
↓ right pane ↓
Selected "(•) Enable macro recording (may be limited)"
Clicked OK button
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I recorded a macro to see what the Libre-Office BASIC would be in terms of accessing objects.
Tools menu → Macros → Record Macro
A “Record Macro” dialog was displayed with a button labeled, “Stop Recording”
The above dialog is a small dialog with a single button on it.
The “Record Macro” dialog is separate from the Libre Writer window.
With the macro recorder running within Libre Writer, I navigated to the document-properties dialog via the below menu items,
File menu → Properties → General tab → OK button
NOTE: Clicking the Cancel button (instead of the OK button) in the above scenario prevented the recorded macro from being saved.
Within the “Record Macro” dialog, I clicked the Stop Recording button
The “BASIC Macros” dialog was displayed for me to “Save” the recorded macro.
Within the “Macro Name” field (shown below), I entered a name for the recorded macro,
Macro Name: RecordDocPropNav
I clicked the Save button
The “BASIC Macros” dialog disappeared.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
I used Alt+F11 to display the “BASIC Macros” dialog.
I clicked on the “RecordDocPropNav” macro within the list in the right pane, below
↓left pane↓ ↓right pane↓
Macro From Existing Macros In: Module1
· · · · · · · · · · · · · · · · · · · · · · · ·
My Macros ...
Module1 RecordDocPropNav
...
I clicked the Edit button
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
The Libre-Office BASIC IDE Editor is displayed.
FYI: The window title for the Libre-Office BASIC IDE Editor is,
"My Macros & Dialogs.Standard —— LibreOffice Basic"
Within the Libre-Office BASIC IDE Editor the cursor was already located at the relevant procedure name, “RecordDocPropNav”.
The relevant code shown below,
sub RecordDocPropNav
rem --------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem --------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
end sub
Within the Libre BASIC IDE Editor, I displayed the “Watched Expressions” window using the following menu items,
View menu → Watched Expressions
I copied the text “document” from the above BASIC code and pasted it into the textbox labeled “Watch:” within the “Watched Expressions” window at the bottom of Libre BASIC IDE window.
Pressing ENTER causes “document” to be displayed within in the “Variables” column (1 of 3) of the “Watched Expressions” window at the bottom of the IDE Editor window.
FYI: To remove an item from the “Watched Expressions” window, click on the variable witin the table and click the “Remove Watch” button to the right of the “Watch:” textbox. The button has an eye with an eye lash on it.
I pressed F8 multiple times to move the debug cursor from the procedure name (“RecordDocPropNav”) down thru the code of the sub proc to one line beyond the below statement,
document = ThisComponent.CurrentController.Frame
FYI: F8 is the keyboard shortcut for the “Step Into” button on the toolbar.
Holding the mouse pointer over the buttons in the toolbar will display their names and shortcut keys as a tooltip.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Within the “Watched Expressions” window at bottom of the IDE Editor window, the document-object model for my Writer document could be explored.
I clicked on the right-pointing triangle () to the left of the term “document” within the “Variables” column and navigated down thru the hierarchy shown below,
▼ document
Creator
CurrentComponent
DocumentProperties ↓ data type ↓
CreationDate ← com.sun.star.util.DateTime (see end-note 01)
The “CreationDate” property contains the following member items,
Variable Value Type
· · · · · · · · · · · · · · · · · · · · · ·
NanoSeconds 144336294 ULong
Seconds 18 UShort
Minutes 42 UShort
Hours 16 UShort
Day 21 UShort
Month 2 UShort
Year 2025 Integer
IsUTC False Boolean
###########################################################################
Accessing the document’s “CreationDate” object using BASIC code
rem ················································································
’
’ Below,
’
’ Declarations
’
’ For Humans ↓
’
’ The “createUnoService” method defines a variable of type “DateTime”
’
dim DocCreationDate as Object
DocCreationDate = createUnoService("com.sun.star.util.DateTime")
’
’ Below, assign the document’s CreationDate object to the local variable DocCreationDate
’
DocCreationDate = document.Creator.CurrentComponent.DocumentProperties.CreationDate
’
’ Below,
’
’ Must explicitly declare the destination variables as String.
’
’ Libre-Office BASIC will perform an assignment directly of Numberic to String,
’ no conversion — Str() — necesssary.
’
’ NOT declaring the destination variables as String, the destination variables
’ are default-defined as type Variant/UShort.
’ When the destination variables are default-defined as Variant/UShort types,
’ using Str(DocCreationDate.Seconds) adds a space to the beginning of the
’ stored string, so that Numberic 2 becomes String " 2" (notice the space to
’ the left of the digit 2… not desired… padding single-digit values with
’ a preceeding zero is the goal).
’
’ LEGEND (for below code)
’
’ “dcd” prefix on variable names = document-creation date
’ The prefix makes the variable names distinct / unique, avoids name collisions.
’
’ Below, declare and assign •time• values to local variables.
’
dim dcdSS as String
dim dcdMM as String
dim dcdHH as String
'
' Below, all members are of type Numeric,
'
' Seconds ULong
' Minutes, Hours, Day, Month UShort
' CCYY Integer
'
dcdSS = DocCreationDate.Seconds
dcdMM = DocCreationDate.Minutes
dcdHH = DocCreationDate.Hours
'
' Below, declare and assign •date• values to local variables.
'
dim dcdDD as String
dim dcdMon as String
dim dcdCCYY as String
dcdDD = DocCreationDate.Day
dcdMON = DocCreationDate.Month
dcdCCYY = DocCreationDate.Year
###########################################################################
My view of the world is that there’s always a vanilla way to accomplish a task.
When someone says to me, “Oh… JUST download the Ding Extension… you’ll also need to get the py backend, along with the bash regular-expressions engine” … seems asking for a banana and being told to acquire the entire jungle is not practical.
###########################################################################
End-Notes
01
LibreOffice_ XDocumentProperties Interface Reference
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1document_1_1XDocumentProperties.html