Skip to content Skip to navigation

Connexions

You are here: Home » Content » LibreOffice macro programming

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

LibreOffice macro programming

Module by: Hannes Hirzel. E-mail the author

Summary: 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.

Note:

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 Writer

http://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

How to enable macro recording

Change the settings to allow macro recording. Recording macros is useful to learn about macro programming.

EnableMacroRecording

Example

The example demonstrates how enumeration of paragraphs is done and how 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 Examples

http://help.libreoffice.org/Basic/Basic_Help

Macros in other languages

Examples in other languages

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks