play a wave sound, then stop playing it

MrDoc

Active Member
Joined
Jul 30, 2002
Messages
343
Hello,
I'm using this simple code to play a wav file I loaded into a worksheet:
ActiveSheet.Shapes("Object 1").Select
Selection.Verb Verb = xlclose
I attached the macro to a button and it works. Now I need to stop the wave sound playing, by attaching another macro to a button. Is that possible? What syntax shall I use to stop playing the sound?
Thank you for any help.
Regards,
MrDoc
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
My code is as follows:
Code:
Option Explicit
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

Sub PlayWav()
Dim WavFile As String
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_LOOP = &H8
Const SND_FILENAME = &H20000

On Error Resume Next
WavFile = "\\dsintserver4\v01\brandonsave\sound bytes\jeopardy.WAV"
Call PlaySound(WavFile, 0&, SND_ASYNC Or SND_LOOP)

End Sub

I have no problem playing the wav file. I added the SND_LOOP to keep the file playing until another macro is finished running. My problem is that it doesn't want to stop unless I close the spreadsheet or go into the vb editor and stop the macro manually. I've tried passing a value(wavfile name) as a string and then passing a vbNullString value when I want it to stop. Problem with that is that as soon as I add arguments to the call function I get errors.
Help!!
 
Upvote 0
ecowar said:
My code is as follows:
Code:
Option Explicit
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

Sub PlayWav()
Dim WavFile As String
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_LOOP = &H8
Const SND_FILENAME = &H20000

On Error Resume Next
WavFile = "\\dsintserver4\v01\brandonsave\sound bytes\jeopardy.WAV"
Call PlaySound(WavFile, 0&, SND_ASYNC Or SND_LOOP)

End Sub

I have no problem playing the wav file. I added the SND_LOOP to keep the file playing until another macro is finished running. My problem is that it doesn't want to stop unless I close the spreadsheet or go into the vb editor and stop the macro manually. I've tried passing a value(wavfile name) as a string and then passing a vbNullString value when I want it to stop. Problem with that is that as soon as I add arguments to the call function I get errors.
Help!!

you need to subst. your Wavefilename with Vbnullstring
 
Upvote 0
Dam would have been nice to have seen the finished code
Code:
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Sub Play_The_Sound()
    Call sndPlaySound("C:\Users\DuncR6\Desktop\Test.wav", 1)
    MsgBox "Click OK to stop the sound..."
    Call sndPlaySound(0, 0)
End Sub
The above will start playing the code and put up a simple message box. When "OK" on the message box is clicked it stops the sound. A single windows "ding" sound plays when the sound file is stopped.

I saw one person saying that the function wouldn't work with Windows 7 64-bit, but this indeed does work. They said to use: "Public Declare PtrSafe Function..." so if it doesn't work then give that a shot. I also tried PtrSafe and both ways worked for me.
 
Upvote 0

Forum statistics

Threads
1,224,203
Messages
6,177,123
Members
452,761
Latest member
Yukun

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top