Partilhar via


Solution - Novice Challenge 10: Skip It

Did you like the first slide in this challenge? We hope so. The first slide turned out to where Binder Clippy has been hiding throughout OfficePalooza. We wanted to give Binder Clippy a little bigger role throughout this competition, but, well, there were some contract issues…. As a result, Binder Clippy got demoted from emcee to featured character on a PowerPoint slide.

Note. If you don’t know what we’re talking about, don’t worry about it. We were going to add a link to the real story of Binder Clippy, but someone snuck in and rewrote the tale before we got to it. To be honest, we’re now too embarrassed by the final outcome to show it to you. But trust us, it was a great story.

Really.

But enough about our poor, ill-fated mascot; it’s time to talk about this challenge. This was a tough one. As you probably found out, it’s one thing to start up a slide show from a macro, but it’s a whole ‘nother thing to show the slides in any order other than the order they appear in PowerPoint. But we managed, by doing this:

Public Sub SkipSlides()

 

Dim i As Integer

Dim j As Integer

Dim o As Integer

Dim e As Integer

 

Dim temp1 As Integer

Dim temp2 As Integer

 

Dim strShowName As String

Dim arrOdd() As Integer

Dim arrEven() As Integer

Dim slideCount As Integer

Dim arrAll() As Integer

Dim customShow As NamedSlideShow

 

'Find out how many slides are in the slideshow.

slideCount = ActivePresentation.Slides.Count

 

'Initialize variables

i = 1

strShowName = "Alternate Slides"

temp1 = slideCount / 2 'Divide the slide count by 2

temp2 = slideCount Mod 2 'If there is an odd number of slides, need to account for the odd one

 

'Number of odd-numbered slides (slides 1, 3, etc.) is temp1 plus temp2;

' number of even-numbered slides (slides 2, 4, etc.) is half of the total.

o = temp1 + temp2

e = temp1

 

ReDim arrOdd(o)

ReDim arrEven(e)

ReDim arrAll(slideCount)

 

'Create an array containing the slide ID of each odd-numbered slide.

j = 1

While (i <= o)

    arrOdd(i) = ActivePresentation.Slides(j).SlideID

    j = j + 2

    i = i + 1

Wend

 

'Create an array containing the slide ID of each even-numbered slide.

j = 2

i = 1

While (i <= e)

    arrEven(i) = ActivePresentation.Slides(j).SlideID

    j = j + 2

    i = i + 1

Wend

 

'Add the odd-numbered slides to a single array.

i = 1

j = 1

While (i <= o)

    arrAll(i) = arrOdd(i)

    i = i + 1

Wend

 

'Add the even-numbered slides to the array following the odd-numbered slides.

While (j <= e)

    arrAll(i) = arrEven(j)

        j = j + 1

    i = i + 1

Wend

 

'Set each slide to transition after 2 seconds.

i = 1

While (i <= slideCount)

    With ActivePresentation.Slides(i).SlideShowTransition

        .AdvanceOnTime = msoCTrue

        .AdvanceTime = 2

    End With

    i = i + 1

Wend

 

'Create a custom slide show from the array of odd and even slides.

ActivePresentation.SlideShowSettings.NamedSlideShows.Add strShowName, arrAll

 

'Start the slide show

With ActivePresentation.SlideShowSettings

    .RangeType = ppShowNamedSlideShow

    .SlideShowName = strShowName

    .Run

End With

 

'Delete the custom slide show

For Each customShow In ActivePresentation.SlideShowSettings.NamedSlideShows

    customShow.Delete

Next customShow

 

End Sub

 

As usual, we start out by declaring a bunch of variables that will be used throughout our code. We then have this line of code:

slideCount = ActivePresentation.Slides.Count

All we’re doing here is using the Count property to find out how many slides are in the presentation, then storing that value in the variable slideCount. You’ll see how we use that in a moment. But first let’s look at a couple of variables we need to initialize:

i = 1

strShowName = "Alternate Slides"

 

Here we’re initializing some variables we’re going to need to set up our slide show. We first set the counter variable i to 1 (we’ll use that in some While loops in just a bit). We’ll also be using a named slide show to accomplish this challenge, so we’ve set the slide show name to Alternate Slides.

What exactly is a named slide show, and why are we using it? A named slide show is going to allow us to create a custom slide show without actually rearranging the slides within PowerPoint. It’s sort of a slide show within a slide show. We give this new slide show a name, tell it which slides we want to include, and in what order, then we can refer to that slide show by name and run it.

So let’s continue. We’re going to show all the odd-numbered slides first, then the even-numbered slides. That means that we first need to find out how many of each slide type (odd or even) we have. We start by setting a couple of temporary variables, then using those variables to determine how many odd-numbered slides we have and how many even-numbered slides:

temp1 = slideCount / 2

temp2 = slideCount Mod 2

The variable temp1 holds the number of slides divided by 2. So if there are 7 slides, temp1 is equal to 3. Keep in mind that we’re using integers here, so we don’t have any fractional values, just whole numbers. If there were an even number of slides in the presentation, we could simply divide the number of slides in half and know that one half are odd and one half are even. But we don’t know that, there might be an odd number of slides. To find out we set temp2 to the remainder of slideCount divided by 2, which is what the Mod function does. In our example of 7 slides, the remainder (Mod) of slideCount divided by 2 is 1.

Now we can determine how many even-numbered slides we have and how many odd-numbered slides. We have temp1 (3) even-numbered slides (slides 2, 4, 6) and temp1 plus temp2 (4) odd-numbered slides (3+1=4: slides 1, 3, 5, 7). Here’s how we determined that in our code:

o = temp1 + temp2

e = temp1

When you run a named slide show you need to provide an array that the slide IDs of each slide to show in the order you want to show them. We’re going to create that array by first creating an array of odd-numbered slides and then an array of even-numbered slides. We’ll then put those two arrays together to create the final slide show.

Let’s start with the array of odd-numbered slides:

j = 1

While (i <= o)

    arrOdd(i) = ActivePresentation.Slides(j).SlideID

    j = j + 2

    i = i + 1

Wend

We’re going to loop through the slides while our counter variable i is less than or equal to the number of odd-numbered slides in the presentation. Each time through the loop we add the slide ID of one slide to the array arrOdd:

arrOdd(i) = ActivePresentation.Slides(j).SlideID

Notice we’ve set another counter variable, j, to 1. This variable tells us which slide in the presentation we’re adding to the array. Thus, the first time through the While loop we add the SlideID of slide j (1) to array position i (1). We then increment j by 2 and i by 1:

j = j + 2

i = i + 1

Why did we increment j by 2? You got it: because we’re only adding odd-numbered slides, which means we add every other slide. As a result, the second time through the loop we’ll be adding the next odd-numbered slide, slide 3, to the array.

After we have all the odd-numbered slides in an arrOdd, we reset our counters and do the same with the even-numbered slides, adding them to arrEven:

j = 2

i = 1

While (i <= e)

    arrEven(i) = ActivePresentation.Slides(j).SlideID

    j = j + 2

    i = i + 1

Wend

Notice that our counter j started at 2; that’s because we’re doing even-numbered slides, which means we start with slide 2 and add slides 4, 6, etc. to the array.

Finally, we put these two arrays together into a new array (arrAll), first putting in everything from arrOdd and then everything from arrEven:

i = 1

j = 1

While (i <= o)

    arrAll(i) = arrOdd(i)

    i = i + 1

Wend

 

While (j <= e)

    arrAll(i) = arrEven(j)

    j = j + 1

    i = i + 1

Wend

You might have noticed that we didn’t reset our counter i between the two While loops. That’s because, after we add the odd-numbered slides to the new array, we want to start at the next position in that array and begin adding the even-numbered slides.

Now it’s time to set some properties for our slide show:

i = 1

While (i <= slideCount)

    With ActivePresentation.Slides(i).SlideShowTransition

        .AdvanceOnTime = msoCTrue

        .AdvanceTime = 2

    End With

    i = i + 1

Wend

Here we’re going through each of the slides and setting the transition properties using the SlideShowTransition object. We set the AdvanceOnTime property to msoCTrue, meaning we want the slides to transition based on a set amount of time, then we set the AdvanceTime property to 2, meaning that set amount of time is 2 seconds.

And then it’s time to actually create our named slide show. We do this by calling the Add method of the NamedSlideShows property:

ActivePresentation.SlideShowSettings.NamedSlideShows.Add strShowName, arrAll

We pass two parameters to the Add method: strShowName, the name of our slide show; and arrAll, the array containing the slide IDs of our show.

And now, finally, we can start the show:

With ActivePresentation.SlideShowSettings

    .RangeType = ppShowNamedSlideShow

    .SlideShowName = strShowName

    .Run

End With

We need to set two properties before the slide show can start: RangeType and SlideShowName. We set RangeType to ppShowNamedSlideShow to – yes, that’s right, to specify that we’re going to be running a named slide show. Then we have to specify which named slide show; we do that by setting SlideShowName to the name of the slide show (strShowName). Now all we have to do is call the Run method and our slide show will begin.

When we were testing our own solution, we ran into one problem: if we ran this subroutine to watch our slide show, then enjoyed it so much that we stopped it and ran it again, the code crashed. The reason it crashed was because we called the Add method to create the named slide show. That’s fine, except that, when we ran the code a second time, the Add method realized that a slide show by that name already existed. Because of that, the Add method refused to add another slide show by the same name and the whole thing crashed.

Rather than worry about checking for existing slide shows or making sure each slide show name was unique, we simply deleted all named slide shows at the end of the subroutine, and asked you to do the same:

For Each customShow In ActivePresentation.SlideShowSettings.NamedSlideShows

    customShow.Delete

Next customShow

Here we’re using a For loop to cycle through the collection of named slide shows and delete each one.

Whew! That was a pretty tough challenge for novices, wasn’t it? If Binder Clippy had been here to help it might have been a little easier.

Well, maybe not….