Writing Macro in LibreOffice Impress – Getting Started

3 min

This simple tutorial will show how to create your first Impress presentation macro in LibreOffice.

Macro is used to automate various tasks from simple to complex. Just like other macro tutorials for Calc spreadsheet, it is possible to automate Impress as well using Basic in LibreOffice.


In this tutorial, we will access a simple Impress odp file with two slides. We will also read the contents of the slides.

The Impress odp contents

The tutorial test file contains two slides and both the slides contents two text boxes. All four text boxes contains different texts. We will access the slides, and each text boxes and its contents.

Slide1 - Impress Macro
Slide1 – Impress Macro
Slide 2 - Impress Macro
Slide 2 – Impress Macro

The Macro

First we need to create an object for the Impress presentation. This can be done using the ThisComponent property. ThisComponent property represents the document that basic belongs to. It returns the object of the corresponding document type from where it is being executed.

oDoc = ThisComponent

Once we have the Impress document object ready, we can now access the Slides in the document. To get the slides, we need getDrawPages() method. This method returns a collection of the slides.

oSlideList = oDoc.getDrawPages()

Individual slides from slide collection can be accessed by index or by name. Slides are numbered from 0 to number of slides in the Impress document. First slide can be accessed by index 0 and so on. You can also access the slide using name as well.

oSlide = oSlideList.getByIndex(0) ' Access by Index
oSlide = oSlideList.getByName("Slide1") ' Access by slide name

If you don’t know the slide name Or want to change the slide names in Impress, right-click the slide thumbnail and click rename slide.

Rename Slides - Impress Macro
Rename Slides – Impress Macro

Once we have the slide, we can now access the objects inside the slides. For this tutorial, we have two text boxes in each slide.

SEE ALSO:   Read Selected Text and Select Part of Text in TextField Control Using LibreOffice Basic Macro

To get the first text box in first slide, below can be used.

oItem = oSlide.getByIndex(0)

Similarly, to access all the text boxes, below snippets can be used.

oSlide = oSlideList.getByIndex(0)
oItem = oSlide.getByIndex(0)

oSlide = oSlideList.getByIndex(0)
oItem = oSlide.getByIndex(1)

oSlide = oSlideList.getByIndex(1)
oItem = oSlide.getByIndex(0)

oSlide = oSlideList.getByIndex(1)
oItem = oSlide.getByIndex(1)

You can also check the types of the object (text box for this example) before you access the contents. It is sometimes needed because you may not find the text contents for an image box.

If oItem.ShapeType = "com.sun.star.presentation.TitleTextShape" Then
' your code here
End If

The item’s getString property returns the contents of the textbox.

Same way, you can access all the textbox of the slides.

Here is the output of the entire macro. The contents from the slides shown in message box, you can also use the contents this way as per your needs.

Impress Macro Output1
Impress Macro Output1

Complete Macro

Here is the complete macro used in this article. You can copy this and paste it to Impress macro editor which can be accessed from Tools -> Macros -> Organize Macros -> LibreOffice Basic.

REM ***** BASIC *****

Sub Main
Dim msg
' Get Access to this Impress Document
oDoc = ThisComponent

' Get a List of all Slides
oSlideList = oDoc.getDrawPages()

' Get the First Slide
oSlide = oSlideList.getByName("Slide1")
' 1st slide - text box 1
oItem = oSlide.getByIndex(0)
If oItem.ShapeType = "com.sun.star.presentation.TitleTextShape" Then
msg = "slide 1 - text 1 : " & oItem.getString & Chr(13)
End If
' 1st slide - text box 2
oItem = oSlide.getByIndex(1)
If oItem.ShapeType = "com.sun.star.presentation.TitleTextShape" Then
msg = msg & "slide 1 - text 2 : " & oItem.getString & Chr(13)
End If

'get the second slide
oSlide = oSlideList.getByIndex(1)
' 2nd slide - text box 1
oItem = oSlide.getByIndex(0)
If oItem.ShapeType = "com.sun.star.presentation.TitleTextShape" Then
msg = msg & "slide 2 - text 1 : " & oItem.getString & Chr(13)
End If

' 2nd slide - text box 2
oItem = oSlide.getByIndex(1)

If oItem.ShapeType = "com.sun.star.presentation.TitleTextShape" Then
msg = msg & "slide 2 - text 2 : " & oItem.getString & Chr(13)
End If
msgbox msg
End Sub

We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!

Join our Telegram channel and stay informed on the move.

Also Read

Like it? Share with your friends!


Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, LinkedIn, or send us an email.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest Most Voted
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x