#1
|
|||
|
|||
Conditionally playing a wave file
This is the code I'm trying to use but I'm getting nothing. I'd prefer to set the path to
C:\Users\X\Music\WWE\ Basically whenever M2>0 I'd like the wav file to play. (This is set in Sheet Tab- View Code Declarations) 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 (This part was in a combined PlayWav) Sub PlayWAV() WavFile = ThisWorkbook.Path & "\BoDallasA.wav" Call PlaySound(WavFile, 0&, SND_ASYNC Or SND_FILENAME) End Sub Private Sub Worksheet_Change(ByVal Target As Range) Threshold = 0 If Range("M2").Value > Threshold Then PlayWAV End Sub |
#2
|
||||
|
||||
Put the following in the Worksheet module for the worksheet that contains the range M2. The SpectFolders function allows you to disctate which Special folder you need to address and the code is set to use My Music as requested - regardless of the user. The macro also assumes a numeric value for M2. I have not included the changes for 64 bit Excel.
Code:
Option Explicit Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _ Alias "PlaySoundA" (ByVal lpszName As String, _ ByVal hModule As Long, _ ByVal dwFlags As Long) As Long Private Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long Private Declare Function SHGetSpecialFolderLocation _ Lib "shell32" (ByVal hwnd As Long, _ ByVal nFolder As Long, ppidl As Long) As Long Private Declare Function SHGetPathFromIDList _ Lib "shell32" Alias "SHGetPathFromIDListA" _ (ByVal Pidl As Long, ByVal pszPath As String) As Long Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long) Private Const CSIDL_DESKTOP = &H0 'Desktop Private Const CSIDL_INTERNET = &H1 'Internet Explorer (icon on desktop) Private Const CSIDL_PROGRAMS = &H2 'Start Menu\Programs Private Const CSIDL_CONTROLS = &H3 'My Computer\Control Panel Private Const CSIDL_PRINTERS = &H4 'My Computer\Printers Private Const CSIDL_PERSONAL = &H5 'My Documents Private Const CSIDL_FAVORITES = &H6 '<user name>\Favorites Private Const CSIDL_STARTUP = &H7 'Start Menu\Programs\Startup Private Const CSIDL_RECENT = &H8 '<user name>\Recent Private Const CSIDL_SENDTO = &H9 '<user name>\SendTo Private Const CSIDL_BITBUCKET = &HA '<desktop>\Recycle Bin Private Const CSIDL_STARTMENU = &HB '<user name>\Start Menu Private Const CSIDL_MYDOCUMENTS As Long = &HC Private Const CSIDL_MYMUSIC As Long = &HD '"My Music" folder Private Const CSIDL_MYVIDEO As Long = &HE '"My Videos" folder Private Const CSIDL_DESKTOPDIRECTORY = &H10 '<user name>\Desktop Private Const CSIDL_DRIVES = &H11 'My Computer Private Const CSIDL_NETWORK = &H12 'Network Neighborhood Private Const CSIDL_NETHOOD = &H13 '<user name>\nethood Private Const CSIDL_FONTS = &H14 'Windows\fonts Private Const CSIDL_TEMPLATES = &H15 Private Const CSIDL_COMMON_STARTMENU = &H16 'All Users\Start Menu Private Const CSIDL_COMMON_PROGRAMS = &H17 'All Users\Programs Private Const CSIDL_COMMON_STARTUP = &H18 'All Users\Startup Private Const CSIDL_COMMON_DESKTOPDIRECTORY = &H19 'All Users\Desktop Private Const CSIDL_APPDATA = &H1A '<user name>\Application Data Private Const CSIDL_PRINTHOOD = &H1B '<user name>\PrintHood Private Const CSIDL_LOCAL_APPDATA = &H1C '<user name>\Local Settings\Application Data (non roaming) Private Const CSIDL_ALTSTARTUP = &H1D 'non localized startup Private Const CSIDL_COMMON_ALTSTARTUP = &H1E 'non localized common startup Private Const CSIDL_COMMON_FAVORITES = &H1F Private Const CSIDL_INTERNET_CACHE = &H20 Private Const CSIDL_COOKIES = &H21 Private Const CSIDL_HISTORY = &H22 Private Const CSIDL_COMMON_APPDATA = &H23 'All Users\Application Data Private Const CSIDL_WINDOWS = &H24 'Windows Directory Private Const CSIDL_SYSTEM = &H25 'System Directory Private Const CSIDL_PROGRAM_FILES = &H26 'C:\Program Files Private Const CSIDL_MYPICTURES = &H27 'C:\Program Files\My Pictures Private Const CSIDL_PROFILE = &H28 'USERPROFILE Private Const CSIDL_SYSTEMX86 = &H29 'x86 system directory on RISC Private Const CSIDL_PROGRAM_FILESX86 = &H2A 'x86 C:\Program Files on RISC Private Const CSIDL_PROGRAM_FILES_COMMON = &H2B 'C:\Program Files\Common Private Const CSIDL_PROGRAM_FILES_COMMONX86 = &H2C 'x86 Program Files\Common on RISC Private Const CSIDL_COMMON_TEMPLATES = &H2D 'All Users\Templates Private Const CSIDL_COMMON_DOCUMENTS = &H2E 'All Users\Documents Private Const CSIDL_COMMON_ADMINTOOLS = &H2F 'All Users\Start Menu\Programs\Administrative Tools Private Const CSIDL_ADMINTOOLS = &H30 '<user name>\Start Menu\Programs\Administrative Tools Private Const CSIDL_CONNECTIONS = &H31 'Network and Dial-up Connections Private Const CSIDL_COMMON_MUSIC As Long = &H35 'All Users\My Music Private Const CSIDL_COMMON_PICTURES As Long = &H36 'All Users\My Pictures Private Const CSIDL_COMMON_VIDEO As Long = &H37 'All Users\My Video Private Const CSIDL_RESOURCES As Long = &H38 'Resource Directory Private Const CSIDL_RESOURCES_LOCALIZED As Long = &H39 'Localized Resource Directory Private Const CSIDL_COMMON_OEM_LINKS As Long = &H3A 'Links to All Users OEM specific apps Private Const CSIDL_CDBURN_AREA As Long = &H3B 'USERPROFILE\Local Settings\Application Data\Microsoft\CD Burning 'unused As Long = &H3C Private Const CSIDL_COMPUTERSNEARME As Long = &H3D 'Computers Near Me (computered from Workgroup membership) Private Const MAX_PATH = 260 Private Const NOERROR = 0 Private Function SpecFolder(ByVal lngFolder As Long) As String Dim lngPidlFound As Long Dim lngFolderFound As Long Dim lngPidl As Long Dim strPath As String strPath = Space(MAX_PATH) lngPidlFound = SHGetSpecialFolderLocation(0, lngFolder, lngPidl) If lngPidlFound = NOERROR Then lngFolderFound = SHGetPathFromIDList(lngPidl, strPath) If lngFolderFound Then SpecFolder = Left$(strPath, _ InStr(1, strPath, vbNullChar) - 1) End If End If CoTaskMemFree lngPidl lbl_Exit: Exit Function End Function Private Sub PlayASound(ByVal pSound As String) If Dir(pSound, vbNormal) = "" Then pSound = SpecFolder(CSIDL_MYMUSIC) & "\" & pSound If InStr(1, pSound, ".") = 0 Then pSound = pSound & ".wav" If Dir(pSound, vbNormal) = vbNullString Then Beep Exit Sub End If End If DoEvents sndPlaySound32 pSound, 0& DoEvents lbl_Exit: Exit Sub End Sub Private Sub PlayWAV() PlayASound "BoDallasA.wav" End Sub Private Sub Worksheet_Change(ByVal Target As Range) Dim Threshold As Long Threshold = 0 If Range("M2").Value > Threshold Then PlayWAV End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Insert a text conditionally | deboer | Word | 1 | 05-04-2014 03:35 PM |
changing font size conditionally | newton.rogers | Excel | 1 | 03-18-2014 12:57 PM |
How to conditionally format this? | g4tv4life | Excel | 2 | 03-13-2014 10:58 AM |
Macro to conditionally create or go to worksheet | Reinaldo123 | Excel Programming | 1 | 07-06-2012 07:23 AM |
PPT not playing full music file | gprcinstructor | PowerPoint | 1 | 09-15-2010 11:08 PM |