PowerPoint – Creating a Random Slideshow Quiz – Part 1

I am crazy about films. I love them, it is probably one of my single favourite pastimes. If it is a 1970’s spaghetti western or a modern bit of Sci-Fi, I’m in. We won’t talk about Rom-Coms though.

During the Winter months when there is nothing to do, we as a family like to play board games, we’re old school like that. One day, my daughter came up with an idea for a game where you pick an actor out of a list of actors and then each player has to name as many films they were in within a set amount of time. It is great fun!

The more regularly we played it, the more I wondered if we could start automating the random selection of the actor and timing of the game. Turns out that you can and this set of posts outlines the evolution of the Visual Basic that controls it all.

Generate a Random Number and move to the Slide

The first draft of the script does a nice straightforward pseudo random number generation and then moves to the slide.

Sub rndSlide()
    Dim actSlide As Integer

    Randomize
    actSlide = Int(9 * Rnd) + 2)

    ActivePresentation.SlideShowWindow.View.GotoSlide (actSlide)
End Sub

So go ahead and fire up PowerPoint, hit Alt + F11 to get into the Visual Basic editor and paste the above code into the Module Editor. If you are unfamiliar with the way the Module Editor works, have a read of my post The Basics of Macro Creation in Microsoft Office.

Let me break down the code for you:

Sub rndSlide()
End Sub

The first and last lines are the boundaries of the Subroutine, or Sub for short.

Dim actSlide As Integer

The term Dim is short for Dimension and we use this command to declare our variable and tell the script what type of data it will hold, in this example it is an Integer. If this is not included you will receive a variable not defined compilation error.

Next we generate the Random number:

 Randomize
 actSlide = Int(9 * Rnd) + 2)

There are two important things to bear in mind here. The Randomize statement needs to be included or your sequence of numbers generated will repeat each time it is generated rather than a unique pattern. The other amendment that needs to be made is the number after Int, this needs to be changed from 9 to the number of slides in your Presentation.

Finally we move to the slide number that has been generated:

ActivePresentation.SlideShowWindow.View.GotoSlide (actSlide)

To trigger the code you will need to a add a button pointing to the Macro, as running it without an active slideshow will error. To find out how to do this, have a read of the coincidentally titled Microsoft PowerPoint – Adding action buttons to a Slideshow.

All done, random number generated and we have moved to the slide. Here’s the thing, how do we know what slide we have already used? As we don’t want to repeat do we?

Read on….