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. Like other macro tutorials for Calc spreadsheets, it is possible to automate Impress using Basic in LibreOffice.

Objective

This tutorial 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 slides have two text boxes. All four text boxes contains different texts. We will access the slides and each text box 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 file i.e. odp file we are working on. 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.

Syntax – getDrawPages

com::sun::star::drawing::XDrawPages getDrawPages ()
Returns an indexed container with the service DrawPages.

For this demo, the code snippet which will collect both slides is below:

oSlideList = oDoc.getDrawPages()

Individual slides from the slide collection can be accessed by index or name. Slides are numbered from 0 to the number of slides in the Impress document. The first slide can be accessed by index 0 and so on. You can also access the slide using the 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 access the objects inside the slides. For this tutorial, we have two text boxes on each slide.

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

oItem = oSlide.getByIndex(0)

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

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 object types (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 textboxes of the slides.

Here is the output of the entire macro. The contents from the slides are shown in the 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 the 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

Closing Notes

I hope this clarifies the very basics of Impress presentation macro. If you have any questions, drop a note below.


Arindam

Creator and author of debugpoint.com. Connect with me via Telegram, 𝕏 (Twitter), or send us an email.
Subscribe
Notify of
guest

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

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments