Playing Multimedia Sounds
You can also use an API call to play a sound within your code from a WAV file. The old
macro programming language had a command to do this, but it has not been included in
VBA, or for that matter in Visual Basic itself, which makes it a bit restrictive if you want to
play sound files. If you want to use Multimedia functions within your code to play sounds or
voice files, then the means to play sound files is very crucial. The declaration goes in the
declarations section of a module:
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA"
(ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As
Long) As Long
You can then play the WAV files within your code:
Sub test_sound()
x = PlaySound("c:\windows\media\windows exclamation.wav", 0, 0 )
x = PlaySound("c:\windows\media\tada.wav", 0, 0 )
End Sub
This example plays two standard Windows sounds. Notice that the flags parameter is set to 0
in both cases. This indicates that the sound is played synchronously so that the command is
not handed back to the VBA code until the sound has finished playing.
If you have a microphone on your computer, you can record your own sound effects or
speech onto a WAV file and play them back in this way.
These examples give some idea of the power of API calls within a program. Many books
are available on this subject if you wish to examine the topic further.
Chapter 20: API Calls 263