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.

Introduction

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.

Notes

Text processing with Writerhttp://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Structure_of_Text_Documents

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.

http://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Editing_Text_Documents

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 com.sun.star.awt.Point
Dim oSize As New com.sun.star.awt.Size
 
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("com.sun.star.drawing.EllipseShape")
oEllipseShape.Size = oSize
oEllipseShape.Position = oPoint

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

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.

EnableMacroRecording

Examples

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("com.sun.star.text.TextDocument") 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("com.sun.star.text.Paragraph") Then
            Count = Count + 1
        End If
    Wend
'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("com.sun.star.text.Paragraph") Then
    Enum2 = TextElement.createEnumeration
    CurLine = "<p>"
    
    ' loop over all paragraph portions
    While Enum2.hasMoreElements
      TextPortion = Enum2.nextElement
 
      If TextPortion.CharWeight = com.sun.star.awt.FontWeight.BOLD THEN
        CurLine = CurLine & "<b>" & TextPortion.String & "</b>"
      Else
        CurLine = CurLine & TextPortion.String
      End If
    Wend

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

    Print #FileNo, CurLine
  End If
 
Wend


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

End Sub

(Reference)

References

LibreOffice Basic Exampleshttp://help.libreoffice.org/Basic/Basic_Help

Macros in other languagesExamples in other languages