BUY EXCEL BOOKS ONLINE: 1. VBA & Macros      2. VBA for Modelers      3. Excel 2013 VBA and Macros     
4. Excel VBA for Dummies      5. Excel with VBA & .NET      6. Mastering VBA      7. Excel 2013 Programming

Notifying with Flash Window or Message Beep

To notify the user that some kind of error has occurred or the application needs the user attention, many developers pushes applications to simply produce a sound by using the MessageBeep function or flash the window by using either the FlashWindow or FlashWindowEx function. An application can also use these functions to call attention to an error and then display a message box or an error message containing details about the error.

i. FlashWindow Function - Flashes the specified window one time. It does not change the active state of the window. Flashing a window means changing the appearance of its caption bar as if the window were changing from inactive to active status, or vice versa. (An inactive caption bar changes to an active caption bar; an active caption bar changes to an inactive caption bar.)

Typically, a window is flashed to inform the user that the window requires attention but that it does not currently have the keyboard focus.

ii. MessageBeep Function - Plays a waveform sound. The waveform sound for each sound type is identified by an entry in the registry. After queuing the sound, the MessageBeep function returns control to the calling function and plays the sound asynchronously.

If it cannot play the specified alert sound, MessageBeep attempts to play the system default sound. If it cannot play the system default sound, the function produces a standard beep sound through the computer speaker.

Note: The user can disable the warning beep by using the Sound control panel application.

To send a beep to a client, use the Beep function. The Beep function is redirected to the client, whereas MessageBeep is not. A long time ago, all PC computers shared a common 8254 programable interval timer chip for the generation of primitive sounds. The Beep function was written specifically to emit a beep on that piece of hardware.

On these older systems, muting and volume controls have no effect on Beep; you would still hear the tone. To silence the tone, you used the following commands:
net stop beep
The 8254 chips were also excluded from the design of server computers. The result is that Beep did not work on all computers without the chip. This was okay because most developers had moved on to calling the MessageBeep function that uses whatever is the default sound device instead of the 8254 chip.

VBA Macro

Option Explicit
'Declare API - MeesageBeep library is User32 while Beep library is Kernel32

Private Declare Function MessageBeep& Lib "user32" (ByVal wType As Long)

Private Const MB_ICONASTERISK = &H40&
Private Const MB_ICONEXCLAMATION = &H30&
Private Const MB_ICONHAND = &H10&
Private Const MB_ICONQUESTION = &H20&
Private Const MB_OK = &H0&
'Main Routine to check the program
Sub Test_MessageBeep()
  Dim BeepType As Long, RtnVal As Long
  Select Case Val(InputBox("Please provide a number between 0 to 4.", "Message Beep Example"))
    Case 0
    BeepType = MB_OK
    Case 1
    Case 2
    Case 3
    Case 4
    BeepType = MB_ICONSTOP
  End Select
  RtnVal = MessageBeep(BeepType)
MsgBox "This is a test", BeepType, "Beep Test"
End Sub