Option Explicit
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Sub TestPlayWavFile()
'run this to play a sound wave (.wav) file
Dim sPath As String
'path to wave file - replace with your own
sPath = "C:\Windows\Media\tada.wav"
'test the no-wait feature
PlayWavFile sPath, False
MsgBox "This message box appears during the sound"
'test the wait feature
PlayWavFile sPath, True
MsgBox "This message appears only after the sound stops"
End Sub
Function PlayWavFile(sPath As String, Wait As Boolean) As Boolean
'make sure file exists
If Dir(sPath) = "" Then
Exit Function
End If
If Wait Then
'hold up follow-on code until sound complete
sndPlaySound sPath, 0
Else
'continue with code run while sound is playing
sndPlaySound sPath, 1
End If
End Function
Sub TestOtherSpeechProcs()
'ReadIntegers "0123456789"
'ExcelSpellOut "0123456789"
ExcelSpeak "Any string can be spoken."
End Sub
Function ExcelSpeak(sIn As String) As Boolean
'speaks any parameter string
Application.Speech.Speak sIn,0,0,0
ExcelSpeak = True
End Function
Function ExcelSpellOut(sIn As String) As Boolean
'uses excel's speak function to read a string
'one character at a time
Dim n As Long, m As Long, sS As String
For n = 1 To Len(sIn)
DoEvents
sS = Mid(sIn, n, 1) 'take one character
Application.Speech.Speak sS, 0
Next n
ExcelSpellOut = True
End Function
Function ReadIntegers(sIn As String) As Boolean
'general vba use
'reads digits from parameter number string one by one
'assumes that wav files for 0 to 9 exist as 0.wav to 9.wav
Dim sS As String, sFdr As String, nLen As Long, n As Long
'get folder for wav files
sFdr = "C:\Users\Your Folder\"
'sound out each digit
For n = 1 To Len(sIn) 'loop through number string
sS = Mid(sIn, n, 1) 'take one character
'call wav file using sample value as part of name
PlayWavFile sFdr & sS & ".wav", True
Next n
ReadIntegers = True
End Function