VBA: PLAY and WAIT

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, I would like to create a macro that will:

1) play a WAV file
2) wait 5 seconds
3) play the WAV file again
4) wait 10 seconds
5) play the WAV file again
6) wait 5 seconds
7) play the WAV file again

Thanks!!!
 
Basically, I would like to know the code to play a WAV file and also the code
to have a delay between 2 commands
 
I was able to find this on the internet:

************************************************************

Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Sub PlaySound()
If Application.CanPlaySounds Then
Call sndPlaySound32("C:\Sound.wav", 0)
********** ????? ********** (I wanna wait 5 seconds here)
Call sndPlaySound32("C:\Sound.wav", 0)
********** ????? ********** (I wanna wait 10 seconds here)
Call sndPlaySound32("C:\Sound.wav", 0)
********** ????? ********** (I wanna wait 5 seconds here)
Call sndPlaySound32("C:\Sound.wav", 0)
End If
End Sub

************************************************************

What can I add the replace the:
********** ????? ********** (I wanna wait XX seconds here)
 
Tim suggested

For the "wait" use
application.ontime

Have a look in your help file for ontime.
 
I did but I find this and I tried everything but nothing works:

Example

This example runs my_Procedure 15 seconds from now.
Application.OnTime Now + TimeValue("00:00:15"), "my_Procedure"

This example runs my_Procedure at 5 P.M.
Application.OnTime TimeValue("17:00:00"), "my_Procedure"

This example cancels the OnTime setting from the previous example.
Application.OnTime EarliestTime:=TimeValue("17:00:00"), _
Procedure:="my_Procedure", Schedule:=False
 
I tried using but I get an error message (Argument not optional) 5 seconds
after teh first WAV sound is played:

Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Sub PlaySound()
If Application.CanPlaySounds Then
Call sndPlaySound32("C:\Sound.wav", 0)
End If
Application.OnTime Now + TimeValue("00:00:05"), "sndPlaySound32"
End Sub
 
I FINALLY FOUND IT!!! I need to run this "sound()" macro and it works...

Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Sub PlaySound()
Call sndPlaySound32("C:\Sound.wav", 0)
End Sub
Sub sound()
Call sndPlaySound32("C:\Sound.wav", 0)
Application.OnTime Now + TimeValue("00:00:05"), "PlaySound"
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top