Does anyone know a macro for a random slide button

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

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( "com.sun.star.presentation.PresentationDocument" ) 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 @jimk, 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( "com.sun.star.presentation.PresentationDocument" ) 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.getPresentation()
		If oPresentation.IsRunning() Then	
			
			Dim oPresentationController As Object  :  oPresentationController = oPresentation.getController()
			Dim iSlideCount As Integer  :  iSlideCount = oPresentationController.getSlideCount()
			Dim iRandomIndex%
			If bUseRandomSlideSet Then
				iRandomIndex = aRandomSlideSet( Int( Rnd * ( uBound( aRandomSlideSet ) + 1 ) ) )
			Else
				iRandomIndex = Int( iMinimum_SlideIndex + Rnd * ( iMaximum_SlideIndex + 1 ) )
			End If
			If iRandomIndex >= 0 And iRandomIndex < iSlideCount Then
				oPresentationController.gotoSlideIndex( iRandomIndex )
			End If

		End If
	End If
End Sub

I inserted a PushButton Form Control in the Slide, and attached the above macro to its “Execute Action” event, but unfortunately the PushButton was not clickable during Presentation runtime…

As a workaround i just insert an Image into the Slide, using the menu “Insert : Image...”, then right-click on the Image and select “Interaction...”, then in the listbox “Action at mouse click:” select “Run macro”, then click on the “Browse” button to locate the above macro ( MoveToRandomSlide_During_Presentation ). That image is now clickable during presentation runtime.

HTH, lib

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.

Thanks @jimk. 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.

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.

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 ?

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

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

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.

The following code worked fine for me when the macro was assigned to a button on a slide. Just change the numbers 8 and 11 to fit your need.

Sub MoveToRandomSlide()
REM Jump to a Random Slide during Presentation.
REM Connect this method to an Image “clicked” event.
Dim oPresentation As Object : oPresentation = ThisComponent.getPresentation()
If oPresentation.IsRunning() Then
Dim oPresentationController As Object : oPresentationController = oPresentation.getController()
oPresentationController.gotoSlideIndex(Int(8*rnd) + 11) REM Jump to one of 8 slides after slide 11 (slides 12-19)
End If
End Sub

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

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