Programming Sounds

     

You'll see later in this chapter that there are a number of ways to present information to the user visually. Also, Chapter 13, "Creating Custom VBA Dialog Boxes," shows you how to create dialog boxes and input forms to gather information from the user. However, these visual cues might get lost in the shuffle if the user has a number of windows and programs open at once. In this case, you might need to supplement visual displays with accompanying sounds that will help focus the user's attention. This section looks at various methods you can employ to work with sounds in your VBA procedures.

caution

graphics/caution_icon.gif

Avoid overusing the Beep statement. You need to get the user's attention, but constant beeping only defeats that purpose; most users get annoyed at any program that barks at them incessantly. Good uses for the Beep statement are signaling errors and signaling the end of long operations.


Beeping the Speaker

VBA's most rudimentary form of communication is the simple, attention-getting beep. It's VBA's way of saying "Ahem!" or "Excuse me!" and it's handled, appropriately enough, by the Beep statement.

Although a single beep is usually sufficient, you can use a For... Next loop to sound multiple beeps, as shown in Listing 12.1. (To see the InsertHyperlinks procedure that is called from Listing 12.1, see Listing 12.2.)

Listing 12.1. A Procedure That Runs Another Procedure and Then Beeps the Speaker Three Times
 Sub InsertHeadingHyperlinks()     Dim i As Integer     '     ' Run the InsertHyperlink procedure     '     InsertHyperlinks "Main Heading"     '     ' Signal the end of the procedure with 3 beeps     '     For i = 1 To 3         Beep     Next 'i End Sub 
graphics/note_icon.gif

I use Word as the underlying application for most of the procedures in this chapter. To get the code for these procedures, see my Web site:

http://www.mcfedries.com/ABGVBA/Chapter12.doc


Programming PowerPoint Sound Effects

The Beep statement is useful, but it's primitive. If you're working with PowerPoint and you have the necessary hardware, you can get your presentations to play much more sophisticated sounds.

PowerPoint has a SoundEffect property that lets you assign and play sounds in a presentation. This property is part of the hierarchy of two PowerPoint objects:

  • AnimationSettings ” The sound effect is applied to the animation associated with an object (such as a picture) on a slide.

  • SlideShowTransition ” The sound effect is applied to a slide's transition.

The SoundEffect property returns or sets a SoundEffect object that represents the sound to be played during the animation or transition. To specify a sound, use the ImportFromFile method:

  Object  .SoundEffect.ImportFromFile(  FileName  ) 

Object

The object to which you want to apply the sound.

FileName

The name and path of the sound file.

For example, the following statements import the tada.wav file as the sound effect for the slide named Start (check your computer for the correct location of the tada.wav file):

 Set currSlide = ActivePresentation.Slides("Start") currSlide.SlideShowTransition.SoundEffect.ImportFromFile _     FileName:="C:\Windows\Media\tada.wav" 

The SlideShowTransition object also has a LoopSoundUntilNext property. This property returns or sets whether or not the sound effect for the specified slide loops continuously until the next sound starts. Use True to loop the sound or False to play the sound just once.



Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net