Ask Your Question

does anyone know a macro for a random slide button

asked 2017-12-13 00:24:34 +0200

joey34 gravatar image

Hi. in libreoffice impress I was wondering if anyone knows of any macros or other ways to make an action button go to one of several slides at random. For example if I were to click the button it could take me to any slide from 5-10

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2017-12-13 02:12:04 +0200

librebel gravatar image

updated 2017-12-14 08:01:31 +0200

Hello @joey34,

Please try if the following macro does what you want:

Sub MoveToRandomSlide( Optional iMinimum_SlideIndex%, Optional iMaximum_SlideIndex% )
REM Jump to a Random Slide whose zero-based index is between <iMinimum_SlideIndex> and <iMaximum_SlideIndex> inclusive.
REM You can leave out the parameters to choose a random Slide from all Slides.
    Dim oDoc As Object          : oDoc = ThisComponent
    If oDoc.SupportsService( "" ) Then
        Dim oSlides As Object   : oSlides = oDoc.getDrawPages()
        If IsMissing( iMinimum_SlideIndex ) Then iMinimum_SlideIndex = 0
        If IsMissing( iMaximum_SlideIndex ) Then iMaximum_SlideIndex = oSlides.getCount() ''- 1 REM EDIT 1.
        Dim iRandomIndex%       : iRandomIndex = Int( iMinimum_SlideIndex + Rnd * iMaximum_SlideIndex )
        If iRandomIndex >= 0 And iRandomIndex < oSlides.getCount() Then
            Dim oController As Object   : oController = oDoc.getCurrentController()
            Dim oSlide As Object        : oSlide = oSlides.getByIndex( iRandomIndex )
            oController.setCurrentPage( oSlide )
        End If
    End If
End Sub

EDIT 1 Please Note: i removed the “- 1” from the code since I noticed that the last item was never chosen… Now it is also chosen.

EDIT 2 In order to use this macro from a Toolbar button, there are 2 possibilities:

  1. call the macro directly without any arguments; In that case it will jump to a random Slide chosen from all Slides.
  2. call a helper method that in turn calls the macro with hardcoded arguments; In that case it will jump to a random Slide chosen from a subrange of all Slides, e.g. from 5 to 10:
Sub MoveToRandomSlide_5_10()
    MoveToRandomSlide( 4, 9 ) REM Slides 5 to 10
End Sub

To connect the macro to a Toolbar button:

  1. While in Impress, select the menu “Tools : Customize… : Toolbars”;
  2. Select a Toolbar to add the Button to, e.g. the Toolbar “Slide View”;
  3. Click “Add Command” to create a new Toolbar Button;
  4. In the “Category” listbox, scroll down to “LibreOffice Macros” and expand it by clicking on the small triangle; From there on further locate the module where you stored the macro ( e.g. in “My Macros : Standard : Impress” ) and select it.
  5. in the “Commands” listbox, select “MoveToRandomSlide” ( or “MoveToRandomSlide_5_10”, or any other created helper method );
  6. Click “Add”, then click “Close”.
  7. Back in the Customize dialog, click “OK”.

Now your new button is added to the Toolbar you selected in step 2 above.

You can toggle the Toolbar’s visibility via the menu “View : Toolbars : ”.

EDIT 3 Following the comments by @Jim K, here's a macro that shows how to jump to a random Slide during Presentation runtime. The range limits are hardcoded, must be adjusted on beforehand; Also supports a Random set: just adjust the aRandomSlideSet() array on beforehand:

Sub MoveToRandomSlide_During_Presentation()
REM Jump to a Random Slide during Presentation.
REM Connect this method to an Image "clicked" event.
    If ThisComponent.SupportsService( "" ) Then

        Const iMinimum_SlideIndex% = 4      REM Set your Random range limits here.
        Const iMaximum_SlideIndex% = 9      REM ( Slides 5 to 10 )

        Dim aRandomSlideSet() : aRandomSlideSet = Array( 1,7,13 )   REM OR: Set your Random Set here ( Slides 2,8,14 ),
        Const bUseRandomSlideSet As Boolean = True                  REM and set this Boolean to True.

        Dim oPresentation As Object : oPresentation = ThisComponent ...
edit flag offensive delete link more


Instructions for creating the button and running it, or a link to such instructions, would be helpful. I spent a few minutes trying to get it to work but could not.

Jim K gravatar imageJim K ( 2017-12-13 18:37:15 +0200 )edit

Thanks @Jim K. for your comment; will update my answer to include an instruction for using the macro from a Toolbar button.

Please also note that it might take a few clicks before it actually jumps to another Slide, if the current Slide is among the allowable Slides to jump to. Since it is random, it might select the same slide again.

librebel gravatar imagelibrebel ( 2017-12-13 22:20:01 +0200 )edit

OK, that is clearer now, and it works. However, the OP asked how to do this from an "action button", which I imagined to work in slide show view (if that's the right term), not design mode. The toolbar would not be visible in that case.

Jim K gravatar imageJim K ( 2017-12-14 00:46:39 +0200 )edit

If that's the case it would indeed be interesting, but it would require more information from @joey34. Is the objective to start the Slideshow normally from slide 1 to 4, then move to a random Slide between 5 and 10, and then continue the Slideshow again normally from Slide 11 to the end, or continue from the randomly chosen Slide until the end ?

librebel gravatar imagelibrebel ( 2017-12-14 01:35:49 +0200 )edit

i updated my answer again ( EDIT 3 ) with an example that works during presentation, albeit with a clickable image instead of an action button..

librebel gravatar imagelibrebel ( 2017-12-14 03:52:00 +0200 )edit

OK, now it has all the information I was looking for, and everything works.

Jim K gravatar imageJim K ( 2017-12-14 17:51:29 +0200 )edit

Hey. Sorry for putting that comment in the wrong place before I'm new to this. I tried the second macro out and it works perfectly. Thanks you very much for the help with both macros and fast response it is much appreciated.

joey34 gravatar imagejoey34 ( 2017-12-15 00:36:20 +0200 )edit

answered 2017-12-14 07:03:16 +0200

joey34 gravatar image

Hey. Thank your for the answer. I tried the macro and got it to work for me as well with random slides of a selected range. is there a way for this macro to jump to random slides that arn't within a range like the example of from slide 5-10 but say randomly from a choice of say slide 2 slide 8 and slide 14. Thanks again

edit flag offensive delete link more


Yw @joey34, i updated the method MoveToRandomSlide_During_Presentation() to allow choosing from a Random Set rather than a Range.

librebel gravatar imagelibrebel ( 2017-12-14 08:03:39 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-13 00:24:34 +0200

Seen: 154 times

Last updated: Dec 14 '17