Powerful PowerPoint for Educators: Using Visual Basic for Applications to Make PowerPoint Interactive


Sub line and the text for the ques tion and right answer. Which Button Did I Press?


Download 1.37 Mb.
Pdf ko'rish
bet141/191
Sana08.05.2023
Hajmi1.37 Mb.
#1442581
1   ...   137   138   139   140   141   142   143   144   ...   191
Bog'liq
2.2. Powerful PowerPoint For Educators

Sub
line and the text for the ques tion and right
answer.
Which Button Did I Press?
The above ex ample works very well when you don’t need to keep track of which
answer was cho sen. But what about the ex ample from Chapter 7 in “How Did You
Do: Re porting Re sults to the Teacher”? In that ex ample, each an swer needs to be
stored. Short-answer questions don’t have much of an issue because you already have
to use VBA to check the an swer, so you can eas ily stick the an swer in a variable at that
time. But multiple-choice ques tions are more of a prob lem. In the ex ample in Chapter
7, we had a different pro cedure for each but ton. This is easy to un derstand, but the
amount of code can be over whelming if you have a lot of questions.
Fortunately, there is a VBA trick that can save us. Try as signing the follow-
ing pro cedure to a but ton. In fact, create a slide with several but tons, add dif fer-
ent text to each but ton, and attach this procedure to each button:
Sub WhichButton(answerButton As Shape)
Dim theAnswer As String
theAnswer = answerButton.TextFrame.TextRange.Text
MsgBox ("You chose " & theAnswer)
End Sub
This uses a spe cial trick with pa rameters (see “Pa rameters”) . When a button is
pressed, it can pass the button it self as a pa rameter to the procedure that called it.
Normally, we use VBA to pass parameters (by putt ing them in pa rentheses when 
we call a procedure), but in this case, click ing the button passes the pa rameter.
We just have to set up our pro cedure to store the pa rameter. In this ex ample, we
used the variable
answerButton
. Once we have a pointer to the but ton it self
(i.e.,
answerButton
), we can get the text that is in the but 
ton with
answerButton.TextFrame.TextRange.Text
. If you have put the an swer
in the text of the but ton, you can use that to get the answer that was chosen.
138 More Tricks for Your Scripting Bag


Now we can store the answers for a printable slide with out add ing any extra
code for each multiple-choice ques tion and without add ing very much ex tra
code for each short-answer question. Our code for the simple three-question ex -
ample is a bit longer, but as you add more ques tions, the overall code will be
much shorter. In fact, just like the pre vious ex ample, if you only use mul tiple-
choice questions, you do not have to change the code at all when you add
questions.
The new code fol lows. The 

Download 1.37 Mb.

Do'stlaringiz bilan baham:
1   ...   137   138   139   140   141   142   143   144   ...   191




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling