Excel vba Play Sound when UserForm Button is clicked

zinzah

New Member
Joined
Mar 9, 2010
Messages
49
so...be doing a lot of reading....and i can't figure it out...

Found this code (and other slightly different version) - but this seems to be the closest to actually working )

VBA Code:
Option Explicit

'Declaration for Win10 and Office 64 bit
Public Declare PtrSafe Function sndPlaySound Lib "winmm.dll" _
        Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
           ByVal uFlags As Long) As Long

Sub TestPlayWavFileAPI()
    '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\Ring06.wav"
     'sPath = "C:\Users\Username\desktop\1.wav"
    'test the no-wait feature
    PlayWavFileAPI sPath, False
    MsgBox "This message box appears during the sound"
    
    'test the wait feature
    PlayWavFileAPI sPath, True
    MsgBox "This message appears only after the sound stops"

End Sub

Function PlayWavFileAPI(sPath As String, Wait As Boolean) As Boolean
    'API declaration of sndPlaySound is modified for 64 bit windows
    'and tests well in Excel 2019 vba version 7.1.
    'For earlier versions it might be necessary to remove the word PtrSafe
    'from the declaration, or to consider another format.
    
    '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 btn2_Click()
Call TestPlayWavFileAPI
End Sub

It works fine and will play any .wav file in "C:\Windows\Media\", but i can't seem to get it to play a sound located elsewhere.
I moved it the the desktop for ease while test, but ultimately it will be playing a file from a network location.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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