No announcement yet.

Playing a sound file in windows 7 using VBA

  • Filter
  • Time
  • Show
Clear All
new posts

  • Playing a sound file in windows 7 using VBA

    Does the method to play a sound in VBA described here: work in windows 7? It uses winmm.dll which doesn't come up when I search my pc. Windows explorer IS showing hidden files and protected operating system files. Thank you

  • #2
    Yes, it works on Windows 7 - I have 64 bit, so I needed to change the declare statement slightly:

    'chip pearson:
    Public Declare PtrSafe Function sndPlaySound32 _
        Lib "winmm.dll" _
        Alias "sndPlaySoundA" ( _
            ByVal lpszSoundName As String, _
            ByVal uFlags As Long) As Long
    Const SND_SYNC = &H0        ' (Default) Play the sound synchronously. Code execution
                                ' pauses until sound is complete.
    Const SND_ASYNC = &H1       ' Play the sound asynchronously. Code execution
                                ' does not wait for sound to complete.
    Const SND_NODEFAULT = &H2   ' If the specified sound is not found, do not play
                                ' the default sound (no sound is played).
    Const SND_MEMORY = &H4      ' lpszSoundName is a memory file of the sound.
                                ' Not used in VBA/VB6.
    Const SND_LOOP = &H8        ' Continue playing sound in a loop until the next
                                ' call to sndPlaySound.
    Const SND_NOSTOP = &H10     ' Do not stop playing the current sound before playing
                                ' the specified sound.
    Sub PlayTheSound(ByVal WhatSound As String, Optional Flags As Long = 0)
        If Dir(WhatSound, vbNormal) = "" Then
            ' WhatSound is not a file. Get the file named by
            ' WhatSound from the Windows\Media directory.
            WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
            If InStr(1, WhatSound, ".") = 0 Then
                ' if WhatSound does not have a .wav extension,
                ' add one.
                WhatSound = WhatSound & ".wav"
            End If
            If Dir(WhatSound, vbNormal) = vbNullString Then
                ' Can't find the file. Do a simple Beep.
                Exit Sub
            End If
            ' WhatSound is a file. Use it.
        End If
        ' Finally, play the sound.
        sndPlaySound32 WhatSound, Flags
    End Sub
    Sub ListWavFiles()
        Dim N As Long
        Dim FSO As Object
        Dim FF As Object
        Dim F As Object
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set FF = FSO.GetFolder(Environ("SystemRoot") & "\Media")
        For Each F In FF.Files
            N = N + 1
            Cells(N, 1) = F.Name
            Cells(N, 2) = F.Path
        Next F
    End Sub
    Sub PlayActiveCellSound()
        PlayTheSound ActiveCell.Text
    End Sub
    Check out our new reputation system. Click on the Like button under the post!

    There are 10 types of people in the world. Those that understand Binary and those that dont.

    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25...

    The BEST Lookup function of all time

    Dynamic Named Ranges are your bestest friend



    • #3

      Ger Plante

      Many Thanks for this useful refresher ...
      If you feel like saying "Thank You" for the help received ...You can click on the "Like" icon ...just underneath ... ... in the bottom right corner ...