LibreOffice macro programming
LibreOffice and OpenOffice programs may be automated through macros. Various languages are possible. This module provides some notes and gives links to the relevant web pages.
This module is a draft.


LibreOffice and OpenOffice programs may be automated through macros. Various languages are possible. This module provides some notes and gives links to the relevant web pages.


Text processing with Writer

The document explains how to access paragraphs and character properties, shows how to traverse a document and how to export certain features of the text as HTML.

Dealing with tables, bookmarks, anchors and other text elements

Macros with LibreOffice CalcThe following example illustrates how to get access to the first sheet (numbering starts with 0) and set the text content of cell "A1" accessed by coordinate position. Source

Dim Doc As Object
Dim Sheet As Object
Dim Cell As Object   
Doc = ThisComponent
Sheet = Doc.Sheets(0)
Cell = Sheet.getCellByPosition(0, 0)
Cell.String = "Test"

The sheets are in a collection which you get the following way:

Dim Doc As Object
Dim Sheets As Object
Doc = ThisComponent
Sheets = Doc.Sheets()

LibreOffice Draw macrosLibreOffice Draw offers as well services which macros may use. The following example creates 100 circles.

Sub draw100Circles
dim i as integer
dim j as integer
for i = 0 to 9
  for j = 0 to 9
    drawCircle(i, j)
  next j
next i
End Sub

Sub drawCircle(x as integer, y as integer)

Dim Doc As Object
Dim Page As Object

Dim oEllipseShape As Object
Dim oPoint As New
Dim oSize As New
oPoint.x = (x+1)*1000
oPoint.y = (y+1)*1000
oSize.Width = 1000
oSize.Height = 1000
Doc = ThisComponent
Page = Doc.DrawPages(0)
oEllipseShape = Doc.createInstance("")
oEllipseShape.Size = oSize
oEllipseShape.Position = oPoint

' RGB = Red-Green-Blue
oEllipseShape.FillColor = RGB(x*28,y*28,0)  

End Sub
result of drawing circles

How to enable macro recordingChange the settings to allow macro recording. Recording macros is useful to learn about macro programming.



Example 1In this LibreOffice writer example all paragraphs are enumerated and thus counted. Source

Sub ParaCount
' Count number of paragraphs in a text document
    Dim Doc As Object, Enum As Object, TextEl As Object, Count As Long
    Doc = ThisComponent
' Is this a text document?
    If Not Doc.SupportsService("") Then
        MsgBox "This macro must be run from a text document", 64, "Error"
        Exit Sub
    End If
    Count = 0
' Examine each component - paragraph or table?
    Enum = Doc.Text.CreateEnumeration
    While Enum.HasMoreElements
        TextEl = Enum.NextElement
' Is the component a paragraph?
        If TextEl.SupportsService("") Then
            Count = Count + 1
        End If
'Display result
    MsgBox Count, 0, "Paragraph Count"
End Sub

Example 2In this example in addition to the enumeration of paragraphs an enumeration of the different parts of a paragraph is done and properties of text elements are queried.

' ===== LibreOffice BASIC =====

Sub Main

Dim FileNo As Integer
Dim Filename As String
Dim CurLine As String
Dim Doc As Object   
Dim Enum1 As Object, Enum2 As Object
Dim TextElement As Object, TextPortion As Object

Filename = "c:\temp\text.html"
FileNo = Freefile
Open Filename For Output As #FileNo   
Print #FileNo, "<html><body>"
Doc = ThisComponent

Enum1 = Doc.Text.createEnumeration

' loop over all paragraphs
While Enum1.hasMoreElements
  TextElement = Enum1.nextElement

  If TextElement.supportsService("") Then
    Enum2 = TextElement.createEnumeration
    CurLine = "<p>"
    ' loop over all paragraph portions
    While Enum2.hasMoreElements
      TextPortion = Enum2.nextElement
      If TextPortion.CharWeight = THEN
        CurLine = CurLine & "<b>" & TextPortion.String & "</b>"
        CurLine = CurLine & TextPortion.String
      End If

    ' output the line
    CurLine = CurLine & "</p>"

    Print #FileNo, CurLine
  End If

' write HTML footer 
Print #FileNo, "</body></html>"
Close #FileNo

End Sub



LibreOffice Basic Examples

Macros in other languagesExamples in other languages