play slides in random order

For revision powerpoints I like to play slides in a random order. The code below will shuffle the slides then play them in the new order. Thanks to Jamie Garroch at BrightCarbon for this program.

Option Explicit
Option Base 1
 
'----------------------------------------------------------------------------------
' PowerPoint VBA Macro to run a slide show in a random order.
'----------------------------------------------------------------------------------
' Copyright (c) 2020 BrightCarbon Ltd. All Rights Reserved.
' Source code is provided under Creative Commons Attribution License
' This means you must give credit for our original creation in the following form:
' "Includes code created by BrightCarbon Ltd. (brightcarbon.com)"
' Commons Deed @ http://creativecommons.org/licenses/by/3.0/
' License Legal @ http://creativecommons.org/licenses/by/3.0/legalcode
'----------------------------------------------------------------------------------
' Purpose : Create and run a random slide show.
' Author : Jamie Garroch
' Date : 25MAR2020
' Website : https://brightcarbon.com/
'----------------------------------------------------------------------------------
Sub RandomOrder()
  Dim oSld As Slide
  Dim aSlides() As Variant
   
  On Error Resume Next
   
  With ActivePresentation
    ReDim aSlides(.Slides.Count)
     
    ' Get a list of all of the slide IDs in the presentation
    For Each oSld In .Slides
      aSlides(oSld.SlideIndex) = oSld.SlideID
    Next
     
    ' Reorder the list of slides in a random order
    ShuffleArrayInPlace aSlides
     
    ' Create and run a custom slide show using the random order
    With .SlideShowSettings
      .NamedSlideShows("Random").Delete
      .NamedSlideShows.Add "Random", aSlides
      .ShowType = ppShowTypeSpeaker
      .LoopUntilStopped = msoTrue
      .RangeType = ppShowNamedSlideShow
      .SlideShowName = "Random"
      .Run
    End With
  End With
   
  On Error GoTo 0
   
End Sub
 
'----------------------------------------------------------------------------------
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
' Source: http://www.cpearson.com/excel/ShuffleArray.aspx
'----------------------------------------------------------------------------------
Private Sub ShuffleArrayInPlace(InArray() As Variant)
    Dim N As Long
    Dim Temp As Variant
    Dim J As Long
    
    Randomize
    For N = LBound(InArray) To UBound(InArray)
        J = CLng(((UBound(InArray) - N) * Rnd) + N)
        If N <> J Then
            Temp = InArray(N)
            InArray(N) = InArray(J)
            InArray(J) = Temp
        End If
    Next N

End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: