Close a message box automatically

Many programs display message boxes that automatically close after a period of time, including printer errors and closing outlook. In VB, this is more complicated than it should be, but still possible. Here's how.

'Module Code
Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
'// Message we receive telling us to close the message box
Public Const NV_CLOSEMSGBOX As Long = &H5000&
Public Sub TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    '// this is a callback function.  This means that windows "calls back" to this function
    '// when it's time for the timer event to fire
    '// first thing we do is kill the timer so that no other timer events will fire
    KillTimer hWnd, idEvent
    '// select the type of manipulation that we want to perform
    Select Case idEvent
    Case NV_CLOSEMSGBOX '// we want to close this messagebox after 4 seconds
        Dim hMessageBox As Long
        '// find the messagebox window
        '// change the text to whatever the title of the message box is
        hMessageBox = FindWindow("#32770", "Self Closing Message Box")
        '// if we found it make sure it has the keyboard focus and then send it an enter to dismiss it
        If hMessageBox Then
            Call SetForegroundWindow(hMessageBox)
            '// this will result in the default option being chosen
            SendKeys "{enter}"
        End If
    End Select
End Sub


'Form Code
Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Private Sub cmdShowMsg_Click()
    '// this shows a messagebox that will be dismissed after 4 seconds
   
    '// set the callback timer and pass our application defined ID (NV_CLOSEMSGBOX)
    '// set the time for 4 seconds (4000 microseconds)
    SetTimer hWnd, NV_CLOSEMSGBOX, 4000, AddressOf TimerProc
   
    '// call the messagebox function
    If MsgBox("Watch this message box close itself after four seconds. The printer is out of paper. Retry or Cancel? (Example)", vbRetryCancel + vbDefaultButton1, "Self Closing Message Box") = vbRetry Then
        MsgBox "Retry!"
    Else
        MsgBox "Cancel"
    End If
 
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan