PDA

View Full Version : Random Slide with checkbox



mystre
12-13-2009, 05:29 AM
I'm very new to VBA and I have the jump to random slide portion of the code working.


Sub CommandButton1_Click()

ActivePresentation.SlideShowWindow.View.GotoSlide Int(Rnd * ActivePresentation.Slides.Count) + 1

End Sub

Now what I like to do is put a checkbox on the slide that causes the slide to be skipped when checked. The flow looks like this.

1. Jump to a random slide
2. determine the status of the checkbox on the new slide (checked or unchecked)
3. If the checkbox is checked then jump to another random slide.
4. repeat.


Thanks

John Wilson
12-13-2009, 06:41 AM
The basic code would be to do a loop jumping to the random slide if "CheckBox1" was not ticked (DON'T DO THIS YET THOUGH READ ON!)

Do
i = Int(Rnd * (ActivePresentation.Slides.Count - 1) + 2) 'not slide 1
If Not ActivePresentation.Slides(i).Shapes("CheckBox1").OLEFormat.Object.Value Then
SlideShowWindows(1).View.GotoSlide (i)
Exit Do
End If
Loop
I made the random slide NOT include slide 1

THE PROBLEM is that when all the slides are ticked the loop will go on for ever! So you need to do a check first

Private Sub CommandButton1_Click()
Dim i As Integer
Dim osld As Slide
Dim oshp As Shape
Dim b_check As Boolean
Do
i = Int(Rnd * (ActivePresentation.Slides.Count - 1) + 2) 'not slide 1
'check first
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.Name = "CheckBox1" Then
If Not osld.Shapes("CheckBox1").OLEFormat.Object.Value Then
b_check = True
Exit For 'it's OK stop check
End If
End If
Next oshp
Next osld

If b_check = False Then 'all boxes checked NOT OK
MsgBox "All slides visited"
Exit Do 'jump out
End If
'end check

If Not ActivePresentation.Slides(i).Shapes("CheckBox1").OLEFormat.Object.Value Then
SlideShowWindows(1).View.GotoSlide (i)
Exit Do
End If
Loop
End Sub


NOTE you will get errors if all the slides (except 1) DO NOT have a checkbox.

mystre
12-14-2009, 12:32 AM
Thank you. I'll be incorporating this in my lesson plans for tomorrow.

Have a great day
Dale