Excel/playing a sound

Advertisement


Question
Hi Aiden

Is the following possible?  I have a timer in cell J9.  When the time reaches 10 seconds, I would like the soundfile at this address to play C:\sound.wav.

Is this possible using VBA?

When I place this code into the worksheet I get an error message along the lines of "ambiguous code...:

Private Sub Worksheet_Change(ByVal Target As Range)
   If Range("J9") > Range("G4") Then PlayWavFile "C:\sound.wav", True
End Sub

Could you tell me where I might have gone wrong?

Thanks in advance.

Chris Mitchell

Answer
PlayWavFile isn't a standard VBA procedure, so must be a custom function - so it may be that the problem is there - ExcelTips give an example of a process that should work at
http://www.exceltip.com/general-topics-in-vba/playing-wav-files-using-vba-in-mic




Itís easy to play soundfiles in WAV-format. You only need to know the filename of the sound you want to play,
and decide if you want the macro to wait while the sound plays or not. Here is an example:

Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Sub PlayWavFile(WavFileName As String, Wait As Boolean)
   If Dir(WavFileName) = "" Then Exit Sub ' no file to play
   If Wait Then ' play sound before running any more code
       sndPlaySound WavFileName, 0
   Else ' play sound while code is running
       sndPlaySound WavFileName, 1
   End If
End Sub

Sub TestPlayWavFile()
   PlayWavFile "c:\foldername\soundfilename.wav", False
   MsgBox "This is visible while the sound is playing..."
   PlayWavFile "c:\foldername\soundfilename.wav", True
   MsgBox "This is visible after the sound is finished playing..."
End SubExceltip


This hopefully gives you enough to get this resolved.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aidan Heritage

Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2016 About.com. All rights reserved.