Playsound not functioning as expected

SimoninParis

New Member
Joined
Mar 2, 2012
Messages
25
Hello fellow VBAers,

I hope someone out there can help me.

Recently, I created a small tool/exercise/pedagogical game which works well for me but acts strangely for colleagues. The tool calls a recording (a .wav file) via a Function, the code for which I copied from a forum:

Code:
Private Declare Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As String, _
  ByVal hModule As Long, ByVal dwFlags As Long) As Long

Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000

Sub PlayWAV()
    WAVFile = ClipNo & ".wav"
    WAVFile = ThisWorkbook.Path & "\" & WAVFile
    Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
    
End Sub

Sub PlayWAV2()
    WAVFile = 2 & ".wav"
    WAVFile = ThisWorkbook.Path & "\" & WAVFile
    Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub


Users (language learners) transcribe the recording word/group of words by word/group of words.

Exercises that I have already created function correctly on my PCs and on those of colleagues. I have shared the "master" Excel workbook with colleagues so they can make their own exercises. While I can use it without any glitches, colleagues elsewhere get error messages (in French I am afraid to say):

Un Module protégé contient une erreur de compilation. L’erreur se situant dans un module protégé, elle ne peut pas être affichée.

Cette erreur se produit couramment lorsque le code est incompatible avec la version ou l’architecture de cette application (par exemple, le code dans un document cible des applications Microsoft Office 32 bits, mais il tente de s’exécuter sur Office 64 bits).
La cause et la solution de lÂ’erreur sont les suivantes :
Cause de lÂ’erreur :



  • LÂ’erreur est levée lorsquÂ’une erreur de compilation se produit dans le code VBA Ã* lÂ’intérieur dÂ’un m odule protégé (masqué). LÂ’erreur de compilation spécifique nÂ’est pas exposée car le module est protégé.

Solutions possibles :

  • Si vous avez accès au code VBA dans le document ou le projet, annulez la protection du module, puis réexécutez le code pour afficher lÂ’erreur spécifique.
  • Si vous nÂ’avez pas accès au code VBA dans le document, contactez lÂ’auteur du document afin de mettre Ã* jour le code du module masqué.
Any ideas, please?

Simon
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Do your colleagues sue the saem Excel version?
Do they all have the correct references set in the VBA module
Many things can be the reason.
 
Upvote 0
Do your colleagues sue the saem Excel version?
Do they all have the correct references set in the VBA module
Many things can be the reason.

They all work for the same organisation as me. They all use ThinkCentre PCs (or equivalent), Windows 7, Excel 2010.
 
Upvote 0
Hello fellow VBAers,

I hope someone out there can help me.

Recently, I created a small tool/exercise/pedagogical game which works well for me but acts strangely for colleagues. The tool calls a recording (a .wav file) via a Function, the code for which I copied from a forum:

Code:
Private Declare Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As String, _
  ByVal hModule As Long, ByVal dwFlags As Long) As Long

Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000

Sub PlayWAV()
    WAVFile = ClipNo & ".wav"
    WAVFile = ThisWorkbook.Path & "\" & WAVFile
    Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
    
End Sub

Sub PlayWAV2()
    WAVFile = 2 & ".wav"
    WAVFile = ThisWorkbook.Path & "\" & WAVFile
    Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub


Users (language learners) transcribe the recording word/group of words by word/group of words.

Exercises that I have already created function correctly on my PCs and on those of colleagues. I have shared the "master" Excel workbook with colleagues so they can make their own exercises. While I can use it without any glitches, colleagues elsewhere get error messages (in French I am afraid to say):


Any ideas, please?

Simon

They are getting a compilation error because they are using VBA7 ... Try chaning the API declarations as follows :
Code:
#If VBA7 Then
    Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
    Alias "PlaySoundA" (ByVal lpszName As String, _
    ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
[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 Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If
 
Last edited:
Upvote 0
.
The following runs on Excel 2007 / Win 10 :

Code:
Option Explicit




Public Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
'Public Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public stopPlaying As Boolean


Sub snd()
sndPlaySound32 "C:\Windows\Media\Tada.wav", &H0
End Sub
 
Upvote 0
They are getting a compilation error because they are using VBA7 ... Try chaning the API declarations as follows :
Code:
#If VBA7 Then
    Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
    Alias "PlaySoundA" (ByVal lpszName As String, _
    ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
[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 Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If


Thanks, I will try that coding and let you know if it works. [maybe next week before I get any response from colleagues; currently, Friday afternoon before a hot sunny weekend ....)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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