Until now, we have used the MsgBox com mand to pop messages up on the screen. That is its main pur pose. How ever, it can do more. Although it can’t let the user type a mes sage (useInputBox for that), MsgBox can display a few dif- ferent combinations of but tons. If you don’t tell it which but tons to use, it just has an OK but ton. The fol lowing ta ble shows the dif ferent but ton combinations you can use along with the se cret word, which we’ll call a “constant,” to ac cess that com bination. I’ll ex plain the secret word after the table.
But ton(s) Con stant
OK vbOK
OK, Cancel vbOKCancel
Abort, Retry, Ig nore vbAbortRetryIgnore
Yes, No, Can cel vbYesNoCancel
Yes, No vbYesNo
Re try, Can cel vbRetryCancel
We can now use a MsgBox to ask a simple question. We don’t have a lot of choices for the an swers (just the limited choices above), but at least we can ask a yes/no ques tion with a MsgBox. For anything more complicated, just use ac tion buttons on a slide and skip the MsgBox.
To put more but tons in aMsgBox, we need to do two things: add a second ar gu ment to theMsgBox com mand (that’s where the se cret word co mes in) and store the an swer in a variable. Because the user can press one of two or three but- tons, we need a way to keep track of which but ton was pressed. For example:
whichButton = MsgBox("Do you like choc olate?", vbYesNo)
The variable whichButton will store in formation about which but ton was pressed, and the sec ond pa rameter toMsgBox (af ter the mes sage that is to ap pear 66 A Scripting Bag of Tricks
and the comma) is the constant that tells MsgBox which but tons to use. Fig ure 6.1 shows the MsgBox.
Fig ure 6.1.MsgBox with Yes and No Buttons
The se cret words are called constants because they represent constant val- ues (unlike variables, which can change value). In this case, the constants are mne mon ics for num bers. For ex am ple, vbYesNo is really the num ber 4. Wher- ever you see the constantvbYesNo, you could type 4 in stead. However, it might be eas ier to re member thatvbYesNo means “I want Yes and No buttons” than remembering what 4 means in a MsgBox com mand. You can make your own constants, but we’ll just use the ones that come with VBA; these usu ally start with the let tersvb (for Vi sual Ba sic) or mso (for Microsoft Of fice), so if you ever see something that starts with vb or mso, it is prob ably a constant.
VBA co mes with hun dreds of con stants that can be used with different commands, and it co mes with a few more for theMsgBox com mand. The most important ones are values returned by MsgBox de pending on which but ton was pressed. The following are the possible values:vbOK, vbCancel, vbAbort,
vbRetry,vbIgnore,vbYes, and vbNo. For ex ample, if the user clicks the Yes but ton, MsgBox re turnsvbYes. We might want to do something based on the button pressed. For example:
'Ask if you like choc olate. Give an ap propriate re sponse. Sub Choc o late()
Dim chocolateAnswer
chocolateAnswer = MsgBox("Do you like choc olate?", vbYesNo) If chocolateAnswer = vbYes Then 'The user likes choc olate. MsgBox ("I like choc olate, too.")
Else 'The user does not like choc olate. MsgBox ("Va nilla is a good choice.") End If
End Sub
Here is an ex ample for a commonly used feature: a quit button. Sometimes users ac cidentally choose quit (by clicking on a but ton that calls a pro cedure with ActivePresentation.SlideShowWindow.View.Exit). To prevent quitting your presentation by accident, you might want to ask if the user re ally wants to quit. As sociate the following pro cedure with your quit but ton:
'Ask if you are sure you want to quit. If the an swer is Yes, 'exit the pre sentation. If the an swer is No, go to the next slide. Sub QuitOK()
're sult is a vari able to keep track of which but ton is clicked. Dim re sult
'MsgBox re turns (will set the vari able re sult to) vbYes if the 'Yes but ton is clicked and vbNo if the No but ton is clicked. re sult = MsgBox("Are you sure you want to quit", vbYesNo) If re sult = vbYes Then 'Was the Yes but ton clicked? ActivePresentation.SlideShowWindow.View.Exit Else 'Since Yes was n’t clicked, it must be No ActivePresentation.SlideShowWindow.View.Next End If
End Sub
With the ad ditional power of MsgBox, you have an other tool to do something based on the answers to simple questions. By combining this with navigational com - mands from the previous section, you can let the user go anywhere in your presenta- tion based on the an swers to questions. But moving from slide to slide isn’t the only response. You might want to stay on the same slide and have something magical hap - pen. In the next section, you will learn how to make ob jects appear and disappear.
Hiding and Show ing PowerPoint Ob jects
In PowerPoint, ev ery ob ject that you see on the screen (text boxes, but tons, pictures, etc.) has sev eral prop erties that can be controlled by VBA. These might include the height and width of the ob ject, the text displayed in the ob ject, the color of the ob ject, etc. An other property is whether or not the ob ject is visible. If you want to be able to see the ob ject, you can set its Vis i ble prop erty toTrue
(note that VBA has a value that is msoTrue; this is the same as True for all your purposes, so don’t worry if the VBA Ed itor sug gestsmsoTrue; you can use
msoTrue or True and it will work). If you want to hide the ob ject, you set its
Vis i bleprop erty toFalse (or msoFalse). For example, if you want to hide