My daugh ter is learning to read. Although I am gen erally op posed to com - puter use by five-year-olds, my daughter is fascinated with the computer, and I thought I could use it with her to help her read. I took the words she was working with in school and the read ing sen tences her teach ers sent home and cre ated a pre sen ta tion. The pre sen ta tion con tains a few sen tences and a mul ti ple-choice question on each slide. Throughout the year, I added to the slides, but I did not want her to go through each slide every time. I wanted to limit her time on the computer, so I wanted the computer to ran domly select five ques tions for her to answer. The pre sentation uses an array to keep track of which questions have been answered (so no ques tion is re peated in each set of five) and ran dom num- bers to pick which question to present next. The code for this presentation can be found in Figure 8.2.
Figure 8.2. VBA Code for Se lecting Five Questions from a Pool of Questions
This pre sentation con sists of a ti tle slide, a last slide, and as many ques tion slides as we want. The ti tle slide has a but ton that is tied to the GetStarted pro - cedure. The question slides have buttons for right and wrong answers that are tied to the RightAnswer and WrongAnswerpro ce dures, re spec tively. The last slide has a but ton that is hyperlinked to the first slide (no VBA) and plays the ap - plause sound. This version does not keep score.
The key el ements of this pre sentation are the arrayvis ited and the pro ce- dureRandomNext.vis ited has an el ement for each question. Actually, it has an el ement for each slide, but the first and last elements are ignored. The el e- ments are each set to False in Ini tial ize. When a ques tion is an swered cor- rectly, the element of vis ited for that ques tion is set to True in the
RightAnswer pro cedure. In ad dition, one is added to numRead, a variable that keeps track of how many questions have been read.
RandomNext is used to go to the next ques tion in stead of
ActivePresentation.SlideShowWindow.View.Next. In the past, the next question has al ways been the next slide. Now, we want to randomly select a slide, so we can’t simply go to the next slide.RandomNext first checks to see whether we have an swered five or more questions. Just in case the presentation doesn’t have five questions, it also checks to be sure we have n’t an swered as many questions as there are:
If numRead >= numWanted Or numRead >= numSlides - 2 Then
numWanted was set in Ini tial ize to be 5; that is, we want to ask five ques - tions at a time. You can change that number in Ini tial ize if you want to ask more or fewer than five questions at a time, or you can ask the user how many questions to do (see be low).
If we have asked enough ques tions,RandomNext jumps to the last slide. Otherwise, it ran domly picks a new slide to jump to. Randomly picking an other slide is very easy us ingRnd, but we want to make sure we are jump ing to a slide that we haven’t seen yet. First we ran domly pick a slide:
nextSlide = Int((numSlides - 2) * Rnd + 2)
This assigns the randomly chosen slide tonextSlide. The While loop keeps looping as long as we have seen the cho sen slide (vis ited(nextSlide) = True). That is, if we pick slide 7 as our next slide,vis ited(7) will be True if we have seen slide 7, so we will keep loop ing, and pick an other slide with
nextSlide = Int((numSlides - 2) * Rnd + 2). Once we have picked the next slide, we can go there with:
ActivePresentation.SlideShowWindow.View.GotoSlide (nextSlide)
That is all you need to choose a few questions from a pool of questions. To add more ques tions, you don’t have to change any VBA at all; just add more question slides be tween the first and last slide. If you want to ask a dif ferent number of questions, you can either changenumWanted = 5 to an other number in the
Ini tial ize pro cedure, or you can try out the code in the next section. This is a good place to re mind you that you can and should use all the tra di- tional PowerPoint tools at your dis posal. For many of the ques tions I have made 148 More Tricks for Your Scripting Bag
for my daugh ter, I in clude pic tures from clip art for the answers in stead of regu- lar buttons. I also use sounds lib erally. The most important use of sound (aside from the applause at the end) is sound for difficult words or sentences. If I in- clude a word or sentence that might be be yond my daughter’s skills, I add a re - corded sound of me reading the word or sentence. She knows that she can click on any speaker icon to have something read to her. While I am not a big fan of bells and whistles, you should use as many traditional features of PowerPoint as you think are appropriate.