CAPÍTULO III: METODOLOGÍA DE LA INVESTIGACIÓN
3.3. Técnicas e instrumentos de recolección de datos
Just as ob ject num bers can change, slide num bers can change as well. If you are try ing to go to a par ticular slide and you use a slide num ber, you might have a problem if you de lete or in sert slides before that slide. Slide names never change unless you change them. When a slide is created, it is as signed a name (Slide1, Slide2, Slide3, etc.). These names are as signed in the order the slide is in serted, not the order in which the slide is within the pre sentation. For ex ample, if you create a slide, it will be named “Slide1.” If you cre ate an other slide, it will be named “Slide2.” If you create a third slide be tween “Slide1” and “Slide2,” it will be the second slide in the presentation, but it will be named “Slide3.”
If you move slides around a lot, you will have a hard time re membering their names. Use GetSlideName and SetSlideName to find out the name of a slide and change the name of a slide:
Sub GetSlideName()
MsgBox ActiveWindow.View.Slide.Name End Sub
Sub SetSlideName()
Dim slideName As String
slideName = InputBox(prompt:="Type a name for the slide") slideName = Trim(slideName)
If slideName = "" Then
MsgBox ("You did not type any thing. " & _ "The name will re main " & _
ActiveWindow.View.Slide.Name) Else
ActiveWindow.View.Slide.Name = slideName End If
End Sub
These procedures are very sim ilar toGetObjectName and SetObjectName. They also run in Edit View of PowerPoint, so they must be run with the “Mac- ros” op tion from the “Macro” flyout menu of the Tools menu (see Figure 8.1).
Once you have a slide’s name, you can use it in two ways. If you want to ac- cess the slide, such as to hide and show ob jects on it, you can use the name in place of the slide num ber. For ex ample, in “Learn First, Ask Questions Later” in Chapter 7, we wanted to hide the marks on the menu slide to in dicate that those sections of the tu torial had not been visited. We used:
ActivePresentation.Slides(2).Shapes(6).Vis i ble = False
This hides shape 6 on slide 2. If we were to name our menu slide “Menu” and the object to be hidden “MenuMark1,” we could use the fol lowing line in stead:
ActivePresentation.Slides("Menu").Shapes("MenuMark1").Vis i ble = False
It is slightly more dif ficult to jump to a named slide. ActivePresentation. SlideShowWindow.View.GotoSlide re quires a num ber; that is, it cannot use the name of the slide in place of the num ber. Fortunately, we can get the slide number by using the name. To jump to the slide named “Menu,” we could use the fol lowing two lines:
theSlideIndex = ActivePresentation.Slides("Menu").SlideIndex ActivePresentation.SlideShowWindow.View.GotoSlide (theSlideIndex)
Although this is a lit tle more com plicated than sim ply us ing a num ber, it is a lot safer be cause slide names never change unless you change them.
You never have to use ob ject names or slide names. You can do ev erything you want with numbers. How ever, as you make more com plicated presentations with more slides and more ob jects, and you be gin to change slides and ob jects around, using names will save you a lot of grief. When you move ob jects, delete slides, reorder slides, in sert slides, change the animation or der of ob jects, etc., your slide names will remain the same, and your VBA code will continue to work.
Ar rays
Computer programs can use many dif ferent kinds of data structures. Un der- standing data structures is an im portant part of computer programming. How- ever, through out this book I have avoided turn ing you into a pro grammer and only shown you what you need to know to be a scripter. The topic of data struc- tures is something you can avoid, but if you un derstand some ba sic data struc- tures, they can make your life easier. In fact, some of the ex amples that you have seen could have been simpler with some more ad vanced data structures. I have made some earlier ex amples lon ger so that they would be easier to understand. Data structures are a way to store in formation. In Chapter 5 we used the box analogy to show how variables can be used to store in formation, but sometimes information can be stored more easily in something other than a sin gle box. A collection of numbered boxes might be more suitable. This collection of num- bered boxes is an array. You might think of an array as an egg carton, with sec- tions for each of several eggs.
In several ear lier examples, such as the ex ample in “Try Again and Again: Answer Again Af ter It’s Right” from Chapter 7, we created our own numbered variables. In that ex ample, we used q1Answered and q2Answered to store the information about whether question 1 was an swered and whether question 2 was answered. If we had more questions, we would add more variables. This is easy to un derstand but dif ficult to type, particularly if we have a lot of questions. This could be simplified with an array.
The first step is to de clare the ar ray. Suppose we have five questions. With - out an array we would do the following to de clare our five variables:
Dim q1Answered As Boolean Dim q2Answered As Boolean Dim q3Answered As Boolean Dim q4Answered As Boolean Dim q5Answered As Boolean
If we were to use an array, we would have one line:
Dim qAnswered(5) As Boolean
This will give us an array that con tains six boxes, numbered 0 through 5:
qAnswered(0), qAnswered(1), qAnswered(2), qAnswered(3),
qAnswered(4), and qAnswered(5). Note that we really only need five boxes in our ex ample, and we got six. There are many ways to avoid getting the extra box, but un less you are an aspiring pro grammer, the easiest thing to do is simply ig nore box number 0.
Now, we can shorten our Ini tial ize pro cedure. It won’t be shorter with two questions (or sig nificantly shorter with five), but when you create some- thing with ten or twenty questions it will be much shorter:
Sub Ini tial ize() Dim i As Long numCorrect = 0 numIncorrect = 0 For i = 1 to 5 qAnswered(i) = False Next i End Sub
This procedure uses aFor loop, just like what we saw above in “Looping.” It loops through each of the members of the qAnswered ar ray and sets each to
False. In the orig inal version, every time you added a new question, you would need to add a newDim state ment and a new line inIni tial ize. Now, the only thing you have to change is the number “5” in your Dim state ment and in the For
line of your Ini tial izepro ce dure.
Having a separate vari able for each question was only a lit tle inconvenient. The big gest in con ve nience was hav ing a sep a rate RightAnswer and
WrongAnswer pro cedure for each ques tion. We needed this
1. to assignTrue or False to the cor rectqAnswered vari able; 2. to know which question was be ing an swered so we could know which
was the ap pro pri ateqAnswered vari able for number 1; and
3. in later ex amples, to as sign the ac tual answer to the correctan swer
vari able.
Our array takes care of num ber 1. Number 2 can be handled easily if our questions are all in or der. In our ex amples with the questions beginning on slide 2, each question is one less than the slide num ber (i.e., question 1 is on slide 2, question 2 is on slide 3, etc.), so to get the ques tion num ber, we simply sub tract one from the slide number (ActivePresentation.SlideShowWindow. View.Slide.SlideIndex - 1). We’ll take care of num ber 3 in the next section.
Us ing theDim state ments andIni tial ize pro cedure from above and the
GetStarted, YourName, DoingWell, and DoingPoorly pro ce dures from any of the earlier examples, we can use the followingRightAnswerpro ce dure andWrongAnswer procedure to re place all the specializedRightAnswer and
WrongAnswer pro cedures. The only thing you ever have to change is the num- ber5 in the Dim state ment and theIni tial ize pro cedure. Just make this num- ber equal to the num ber of questions you have.
Sub RightAnswer()
Dim thisQuestionNum As Long thisQuestionNum = _
ActivePresentation.SlideShowWindow.View.Slide.SlideIndex - 1 If qAnswered(thisQuestionNum) = False Then
numCorrect = numCorrect + 1 End If qAnswered(thisQuestionNum) = True DoingWell End Sub Sub WrongAnswer()
Dim thisQuestionNum As Long thisQuestionNum = _
ActivePresentation.SlideShowWindow.View.Slide.SlideIndex - 1 If qAnswered(thisQuestionNum) = False Then
numIncorrect = numIncorrect + 1 End If
qAnswered(thisQuestionNum) = True DoingPoorly
End Sub