Button to play wav file based on cell value

zackete

New Member
Joined
Apr 15, 2012
Messages
42
Office Version
  1. 365
Hello everyone!

Well, let's say that I've just arrived to this VBA excel world. One week ago I have no idea that microsoft excel had such a cool feature...

Somehow I've managed (thx google!) to insert an extremely helpful VBA code on one of my excel files.

I'm using Microsoft Excel 2010 on Windows 7 64 bits.

This code looks on 2 different columns, and plays a .wav file based on the cell value.

Code:
Private Declare PtrSafe 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

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
On Error Resume Next
If Target.Column = 1 Then
Call PlaySound("C:\Users\Diego\Desktop\" & Target.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
Else
Call PlaySound("D:\" & Target.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
End If
On Error GoTo 0
End Sub
It works perfect, no issues at all. But I'm looking to create also a clickeable button, that plays the .wav file based on the cell selected.

So if for example the cell contains the value "ladygaga", when you click the button it plays the audio ladygaga.wav.

Any help with this would be really much appreciated :)

Thx in advance!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
The macro that the button is assigned to/triggers should contain:
Code:
On Error Resume Next
  Call PlaySound("C:\Users\Diego\Desktop\" & ActiveCell.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
On Error GoTo 0
??
 
Upvote 0
So you mean like this?

Code:
Private Sub Play_Click()
On Error Resume Next
  Call PlaySound("C:\Users\Diego\Desktop\" & ActiveCell.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
On Error GoTo 0
End Sub

Just tried it, but didn't worked :S
 
Upvote 0
Got your point! I made it works with the following code:

Code:
Private Declare PtrSafe 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
Private Sub Play_Click()
  Call PlaySound("C:\Users\Diego\Desktop\" & ActiveCell.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
End Sub
But, if there are 2 wav files with the same name in column 1 and 2, it always plays the one allocated on "C:\Users\Diego\Desktop\".

I want that if you click on a .wav file at Column2, it looks for it on a different path (i.e: "D:\").

Thx in advance and thx for help! :)
 
Upvote 0
..then either have the path included in the cell and remove it from the macro or if you know that cells from a given column will always play from a given folder you can do something like this:
Code:
Private Sub Play_Click()
Select Case ActiveCell.Column
  Case 1
    Call PlaySound("C:\Users\Diego\Desktop\" & ActiveCell.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
  Case 2
    Call PlaySound("D:\somefolder\somewhere\Desktop\" & ActiveCell.Value & ".wav", 0&, SND_ASYNC Or SND_FILENAME)
End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

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