Reputation: 908
VBA is not my strength, yet I would like to get a Excel sheet where A1 might contain the word "Hello" or "World" or the number 1, and depending on that content a sound file should be played when clicking a button that is next to it. The wav files are located in a specific subfolder where the excel file is.
I’m using Excel (64 bit) version. I was already checking this page: How to play a wave file or sound file using VBA
And tried the following (64 bit compatible):
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
#Else
Public Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If
Sub PlayTheSound(ByVal WhatSound As String)
If Dir(WhatSound, vbNormal) = "" Then
' WhatSound is not a file. Get the file named by
' WhatSound from the Windows\Media directory.
WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
If InStr(1, WhatSound, ".") = 0 Then
' if WhatSound does not have a .wav extension,
' add one.
WhatSound = WhatSound & ".wav"
End If
If Dir(WhatSound, vbNormal) = vbNullString Then
Beep ' Can't find the file. Do a simple Beep.
Exit Sub
End If
Else
' WhatSound is a file. Use it.
End If
sndPlaySound32 WhatSound, 0& ' Finally, play the sound.
End Sub
Sub PlayIt()
Select Case Range("A1").Value
Case "Hello"
PlayTheSound "chimes.wav"
Case "World"
PlayTheSound "chord.wav"
Case 1
PlayTheSound "tada.wav"
End Select
End Sub
Yet, I get an error
Argument not optional
Can anyone help me with that?
Upvotes: 1
Views: 2587
Reputation: 19289
The error is caused because you are calling the function with 2 arguments when the function expects 3. If you want to call sndPlaySound32
like this
sndPlaySound32 WhatSound, 0& ' Finally, play the sound.
Then you need you need to change the function signatures to have just the 2 arguments per the question you referenced - like this:
#If VBA7 Then
Public Declare PtrSafe Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal dwFlags As Long) As Long
#Else
Public Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal dwFlags As Long) As Long
#End If
So the full working code is:
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal dwFlags As Long) As Long
#Else
Public Declare Function sndPlaySound32 Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal dwFlags As Long) As Long
#End If
Sub PlayTheSound(ByVal WhatSound As String)
If Dir(WhatSound, vbNormal) = "" Then
' WhatSound is not a file. Get the file named by
' WhatSound from the Windows\Media directory.
WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
If InStr(1, WhatSound, ".") = 0 Then
' if WhatSound does not have a .wav extension,
' add one.
WhatSound = WhatSound & ".wav"
End If
If Dir(WhatSound, vbNormal) = vbNullString Then
Beep ' Can't find the file. Do a simple Beep.
Exit Sub
End If
Else
' WhatSound is a file. Use it.
End If
sndPlaySound32 WhatSound, 0& ' Finally, play the sound.
End Sub
Sub PlayIt()
Select Case Range("A1").Value
Case "Hello"
PlayTheSound "chimes.wav"
'PlayTheSound "chime.wav"
'Case "World"
'PlayTheSound "chord.wav"
'Case 1
'PlayTheSound "tada.wav"
End Select
End Sub
Upvotes: 1