Stop .wav file with button after started playing

mewingpants

New Member
Joined
Apr 1, 2019
Messages
12
Just had a question that I googled and searched this forum and every where else but could not find a solution anywhere.

So I am using this code to help me play a .wav file

Code:
#If Win64 Then
    Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
        Alias "PlaySoundA" (ByVal lpszName As String, _
        ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function PlaySound Lib "winmm.dll" _
        Alias "PlaySoundA" (ByVal lpszName As String, _
        ByVal hModule As Long, ByVal dwFlags As Long) As Boolean
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Function SoundMe() As String
'Updateby Extendoffice 20161223
    Call PlaySound("siren.wav", _
      0, SND_ASYNC Or SND_FILENAME)
    SoundMe = ""
End Function

I then use this code to play the .wav file if E15 is greater than 146772

Code:
=IF(E15>146772,SoundMe(),"")

This all works great, when E15 is greater than 146772 the .wav plays as it should but I would just like to add a button to "Acknowledge" the alarm and stop the siren sound from playing, if possible.

It is not set on loop but it is a long siren sound and it will go away after 15 seconds but I just wanted to add a "ack" button so whoever puts in a value that's to high must push the 'ack' button. This way they are completely aware of why the alarm is going off.

Sorry if this question was already asked but I couldn't find anywhere this particular set up.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Oh yes excellent link lots of information :)

Not quite sure how to use the code exactly, I have tried

Code:
Private Sub CommandButton28_Click()
    PlaySound vbNullString, 0, 0
End Sub

Code:
Private Sub CommandButton28_Click()
    Call andPlaySound (0, 0)
End Sub

Keep getting Sub or Function not defined

Am I putting this in the module completely wrong? I have tried many combinations.
 
Upvote 0
I expect Private Sub CommandButton28_Click is in a sheet module as the handler for an ActiveX command button and the code in your OP is in a separate standard module (e.g. Module1). If so, the error is caused by the scope of the PlaySound function. PlaySound is declared as Private, so it is visible only to procedures in Module1. For more info see https://docs.microsoft.com/en-us/of...ng-started/understanding-scope-and-visibility.

There are 2 solutions, either:

1. Change both PlaySound declarations from Private to Public.

Or:

2. Define a Public procedure in Module1:

Code:
Public Sub StopSound()
    PlaySound vbNullString, 0, 0
End Sub
and call it from the Click handler:
Code:
Private Sub CommandButton28_Click()
    StopSound
End Sub
 
Last edited:
Upvote 0
Ahh yes the Public and Private make total sense.

So the

Code:
[LEFT][COLOR=#333333][FONT=monospace]Private Sub CommandButton28_Click()
    StopSound
End Sub[/FONT][/COLOR][/LEFT]

Works great, it calls StopSound but I get the same Sub or Function Not Defined error with the

Code:
[LEFT][COLOR=#333333][FONT=monospace]Public Sub StopSound()
    PlaySound vbNullString, 0, 0
End Sub[/FONT][/COLOR][/LEFT]

The debug always highlights "Playsound" with that error

Yes you are correct the commandbutton_28 (active x button) is in the Sheet code and the other code in a Module.
 
Upvote 0
Where have you placed StopSound? It should go in the standard module, not the sheet module. Apart from that I can't see why you're getting that error, as long as you've followed either of my solutions. If still no joy put the code in a new workbook.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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