Multiple Apps

PC Speaker Beep

Ease of Use

Easy

Version tested with

2003 

Submitted by:

Oorang

Description:

Sends a beep/custom sound to the PC speaker. 

Discussion:

In corporate or legacy environments it is not unusual to have an environment without sound cards at all, or the workstations my have sound cards but not speakers. This prevents you from giving audio feedback to your users. However most commercial PCs will have speakers installed inside their case to send BIOS beep codes. These examples will show you how to force output to the built in PC speakers thereby allowing audio feedback. Note: These will bypass the sound card if one is present. If you wish to send sound via the sound card please article by Justin La Benne http://vbaexpress.com/kb/getarticle.php?kb_id=771. (Or simply use the standard "Beep" function.) (Thanks to lucas for testing this on a box with no PC Speakers.) 

Code:

instructions for use

			

Option Explicit Private Declare Function BeepA Lib "kernel32" Alias "Beep" ( _ ByVal Frequency As Long, ByVal Duration As Long) As Long Public Sub CustomBeep() Dim intFrq As Integer Const lngStep_c As Long = 150 Const lngMillisecond_c As Long = 1 Const lngUprBnd_c As Long = 6000 Const lngLwrBnd_c As Long = 100 For intFrq = lngLwrBnd_c To lngUprBnd_c Step lngStep_c PCSpeakerSound intFrq, lngMillisecond_c Next For intFrq = (lngUprBnd_c - lngStep_c) To (lngLwrBnd_c + lngStep_c) Step -lngStep_c PCSpeakerSound intFrq, lngMillisecond_c Next End Sub Public Sub PCSpeakerBeep() 'Note: This function is asynchronous. On Error Resume Next Const strCommand_c As String = "cmd /c echo " Const lngBellChr_c As Long = 7 VBA.Shell strCommand_c & VBA.Chr$(lngBellChr_c), vbHide End Sub Public Sub PCSpeakerSound(Frequency As Integer, Duration As Long) 'Purpose: Sends as sound to the PC speaker 'Input : -Frequency: Specifies the frequency (in hertz) ' of the sound to be sent to the speaker. Only ' accepts values from 37 through 32,767. ' -Duration: Length of the sound in milliseconds. Const lngValidLwrBnd_c As Long = 37 Const lngValidUprBnd_c As Long = 32767 Const strError_c As String = "Invalid value for parameter" & _ """Frequency"". Values must " & _ "be 37 through 32,767." If Frequency < lngValidLwrBnd_c Then VBA.Err.Raise vbObjectError, VBA.Err.Source, strError_c ElseIf Frequency > lngValidUprBnd_c Then VBA.Err.Raise vbObjectError, VBA.Err.Source, strError_c Else If BeepA(Frequency, Duration) = False Then VBA.Err.Raise vbObjectError, "PCSpeakerSound", "Speaker not found." End If End If End Sub

How to use:

  1. Press Alt F-11 to launch the Visual Basic Editor (VBE).
  2. From the insert menu of the VBE insert a Module (not a Class Module).
  3. Copy/Paste above code paste into module.
  4. From the Debug menu select Compile Project.
  5. Save File
 

Test the code:

  1. Run the sub named "CustomBeep".
  2. Run the sub named "PCSpeakerBeep".
 

Sample File:

PCSpeakerSoundExample.zip 12.69KB 

Approved by mdmackillop


This entry has been viewed 158 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express