• Changing the sound of ‘Beep’

    Author
    Topic
    #356630

    Is there any way to change the sound made by the Beep command or to play a different sound? (Office 2000 and Windows 2000)

    The Beep command plays the sound defined as ‘Default Beep’ in Control Panel/Sounds and Multimedia. I can change this manually, but then have to change it back again. I would like to be able to do one of 3 things:
    1. Play a .wav file direct from VBA
    2. Play one of the other sounds defined in Sounds and Multimedia
    3. Change the definition of Default Beep from VBA

    Does anyone know if these are possible?

    Thanks
    Ian.

    Viewing 1 reply thread
    Author
    Replies
    • #528208

      Go for the “Play a .wav” option. Your choices for “beep” are endless…

      Private 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 “Sleigh Ride.wav”, False
      End Sub

    • #528253

      As Kevin has shown you can use the sndPlaySound API call to play any WAV file. However, the more extensive PlaySound call does let you do the same but also lets you, amongst other things, play system event sounds. The following should be in a declaration section of a code module

      Public Declare Function PlaySound Lib "winmm.dll" _
       Alias "PlaySoundA" (ByVal lpszSoundName As String, _
       ByVal hMod As Long, ByVal uFlags As Long) As Long

      lpszSoundName is the sound name you want to play and can be a file, a system event, a sound held in memory or an embedded sound. hMod is used only for embedded sounds and so can be set to zero for all other cases. The Flags argument is used to tell the function how to interpret lpszSoundName, i.e. a Filename (SND_FILENAME) or a System Sound (SND_ALIAS) etc. (The function takes a guess at which one is required if you omit the argument, but it is probably best to include it)

      The following examples show how the function might be used to meet your requirements.

      Sub TestWavFileSound()
          Dim sndFile as String
          sndFile = "C:WINDOWSMEDIATADA.WAV"
          PlaySound sndFile, ByVal 0&, SND_FILENAME 
      End Sub

      As far as I can tell, there is no need to check for the existence of the file, as if it does not exist a default sound is played.

      Sub TestSysEventsSound()
          Dim sysEvent as String
          sysEvent = "Close"
          PlaySound sysEvent, 0&, SND_Alias
      End Sub 

      the following are some ssytem sound identifiers you can pass as arguments :

      	AppGPFault, Close, EmptyRecycleBin, MailBeep, Maximize, Minimize, 
      	Open, RestoreUp, RestoreDown, SystemStart, SystemExit, 
      	SystemHand (critical stop), SystemQuestion, SystemExclamation, 
      	SystemAsterisk, MenuCommand, MenuPopup, and .Default (note the preceeding period). 

      There is another API call, (Declare Function MessageBeep Lib “user32” (ByVal wType As Long) As Long), which is probably not required as it cannot do (as far as I know) anything that PlaySound cannot do.

      As for changing the default beep, I do not know if that is possible, but in Windows NT (and presumably Windows 2000) if you use Declare Function Beep Lib “kernel32” (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long, you can pass the Beep command 2 parameters. The dwFreq parameter specifies the frequency (in Hertz) and must be in the range 37 through 32,767. dwDuration specifies the duration in milliseconds. In Win95 to WinME, these parameters are ignored.

      Andrew C

      • #528260

        Thanks, Kevin and Andrew, that’s just what I needed.

        I didn’t know about “winmm.dll” – where do you find out about such libraries?

        Thanks again
        Ian.

        • #528262

          Ian,

          winmm.dll is Windows’ library of multimedia functions. To find out more about API calls check out AllAPI, where you can download a good reference (API-Guide) and you will find more details about PlaySound and the flags and parameters that can be passed to it.

          regards,

          Andrew

    Viewing 1 reply thread
    Reply To: Changing the sound of ‘Beep’

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: