Library code snippets

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

Comments

  1. 03 Nov 2003 at 14:17

    I could not get it to work in Outlook2000 until I added TimerID as long then..
    TimerID=SetTimer (hWnd, NVCLOSEMSGBOX, 4000, AddressOf TimerProc)
    then change the line
                   Case NV
    CLOSEMSGBOX  into  Case TimerID
    I think NV_CLOSEMSGBOX is not required..
    and all works fine


  2. 03 Jun 2002 at 07:58

    this is way to complicated....


    call me stupid, but couldn't you use this:


    Module:


    Declare Sub Sleep Lib "kernel32" (ByVal dwmilliseconds As Long)


    Form Code:


    Private Sub Form_Load()
    msgbox "The printer is out of paper, sucka!", 64, "Your not very smart, are you?"
    sleep 4000   ' this results in a 4 second delay
    appactivate "Your not very smart, are you?
    sendkeys "{enter}" 'selects default (this is "OK" for a 1 button message box)
    ' other code goes here
    end sub


    For 2 buttons, to select the non-default one, try this:
    appactivate "Your not very smart, are you?"
    sendkeys "{tab}"
    sendkeys "{enter}"


    i know the sleep function pauses all activity within the program, but this seems like it may do the trick,

  3. 07 Dec 2001 at 17:44

    First off Thanks for this code it works well for some message boxes.


    I can change this line :
    hMessageBox = FindWindow("#32770", "Self Closing Message Box")
    to this
    hMessageBox = FindWindow("#32770", "lnternet Explorer Script Error")


    then I use my error handlers to call this line
            SetTimer hWnd, NV_CLOSEMSGBOX, 100, AddressOf TimerProc


    and it works.. I am making a web browser that surf automaticy for sites I am checking for banners. It kills those pesky Message boxes and to close the Confirm message boxes just add this line


    SendKey  "{tab}" above this line in your code
    SendKey  "{enter}"


    This selects NO because usually no is always second on a javascript alert. (That's been my case)


    Now the modifications above work really well but I want to know how to close more because it does not get them all. Example those pesky JAVASCRIPT Error messages.


    I tried to modify the title of that from the error alert but it seems not to work..
    I changed this
    hMessageBox = FindWindow("#32770", "Microsoft Internet Explorer")
    to this:
    hMessageBox = FindWindow("#32770", "lnternet Explorer Script Error")


    but it still dont' work and I can't figure out why. maybe the number above need to be changed ? IF so what does it need to be changed to ?


    I could use some help on this please ! Any help will be usefull..


  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Close a message box automatically.

Leave a comment

Sign in or Join us (it's free).

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 ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback