Embed .WAV file into existing Macro

VinceF

Board Regular
Joined
Sep 22, 2007
Messages
220
Office Version
  1. 2016
Platform
  1. Windows
Help...Really struggling with this, I'm sure it'll be simple for the experts.

I have a Active X button that executes a macro. I'm trying to have a .wav file (click.wav) play when the button is pressed.
With my extremely limited VBA skills I've been unable to make it happen...The code below is where I'm trying to place it into.
Thanks
VinceF
Win10
Office365

VBA Code:
Sub GenerateMultipleRandomintegers()

    

For i = 1 To 3
    Do
'       Calculate number
        n = WorksheetFunction.RandBetween(1, 18)
'       Check to see if number already used in range
        If Application.WorksheetFunction.CountIf(Range("A1:A3"), n) = 0 Then
'           If not duplicate, assign to cell and exit loop
            Range("A" & i) = n
            Exit Do
        End If
    Loop
Next i


   
    
End Sub
 
Let me add a screenshot:

1739826461266.png
 
Upvote 0
If you want to make it easier, you could just play the sound in a hidden Windows Media Player

VBA Code:
Sub test()
Shell "C:\Program Files\Windows Media Player\wmplayer c:\temp\test.wav", vbHide
End Sub
 
Upvote 0
Thank you to everyone who attempted to help me with this request. When I run the above code it tells me that I have to update to 64 bit. I'm running Win10 64 bit, office365 all updated etc.
All this is way over my head and way more involved and work that I had anticipated, I'm going to let this one pass.
Thanks again,
VinceF
 
Upvote 0
Good morning
the sndplaysound api is incorrectly declared
you said you work with W10 and office 365
so you are in vba7 and maybe even on 64 bit
you therefore need a compliant API declaration
the sbnplaysound api has the advantage of playing the wave file asynchronously
VBA Code:
Option Explicit
'patricktoulon( france )
'https://excel-downloads.com/members/patricktoulon.167882/#resources

'you can use in alls office excel version
#If VBA7 Then
   'office 2010 to  2024
   Private Declare PtrSafe Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
#Else
   'vba office 2003 to 2007
   Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If

Private Const SND_ASYNC As Long = &H1 'play asynchrone
Private Const SND_FILENAME As Long = &H20000 'default


Sub GenerateMultipleRandomintegers()
  Dim i&, N&
 'you can start to play  a sound before because  it playing asynchrone
 'the code will continue to execute while the sound is playing
PlaySound "C:\Windows\Media\Windows Notify Calendar.wav", 0, SND_ASYNC Or SND_FILENAME
 
 For i = 1 To 3
      Do
'        Calculate number
         N = WorksheetFunction.RandBetween(1, 18)
'        Check to see if number already used in range
         If Application.WorksheetFunction.CountIf(Range("A1:A3"), N) = 0 Then
'           If not duplicate, assign to cell and exit loop
            Range("A" & i) = N
            Exit Do
         End If
      Loop
   Next i
'I chose a wave sound integrated into w10(in windows\media) folder; because  I find it very elegant
     
End Sub
tested and work fully

have a nice day
patrick;)
 
Upvote 0
Solution
Patrick,

I had given up on getting this to work.
Your solution is working perfectly..
Thank you very much.......!!!!

VinceF
 
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,926
Members
453,767
Latest member
922aloose

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