VBA MsgBox timer?

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,462
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
Hello Everyone
Is it possible to have a timer on a VBA message box such that it disappears after a set interval? I have a standard VBA MsgBox informing the user that an action was successful - how can I make the message box disappear (as if Ok was pressed) after 3 seconds? Is this possible? I'm new with VB and am using it in conjuntion with Access 2000.
TIA, Andrew :)
 
I know this reply is 10 years later than when the question, above, was asked, but I'm sure some people might be looking of any solution on implementing a timed message box in Excel, as was I, today. Using a couple of sources (which I'll mention, below) I made a combinated VBA code.

What the code does, it makes the message box appear some seconds after you open your Excel file and it makes the message box automatically close after some seconds. Ideal if you want to show a welcome message. Well, here goes:

Put this code inside a module
VBA Code:
Option Explicit
Const PopupDurationSecs As Integer = 3
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:03"), "myShellMessageBox"
End Sub

Sub myShellMessageBox()
    Dim AckTime As Integer, InfoBox As Object
    Set InfoBox = CreateObject("WScript.Shell")
    'Set the message box to close after 10 seconds
    AckTime = 10
    Select Case InfoBox.PopUp("Click OK (this window closes automatically after 10 seconds).", _
    AckTime, "This is your Message Box", 0)
        Case 1, -1
            Exit Sub
    End Select
End Sub

Copy this code in "ThisWorkbook":
VBA Code:
[/B]
Private Sub Workbook_Open()
startTimer
End Sub
[B]

Sources:

Codes for automatically starting the VBA message box: from: Dinesh Kumar Takyar.
Code for automatically closing the VBA message box: from: Microsoft

Hope it helps you guys!
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You probably would have received a response sooner if this was asked on the Excel Questions board, but at any rate here's one way to do what you want:

Sub Test1()
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
AckTime = 3
Select Case InfoBox.Popup("Click OK or do nothing within 3 seconds.", _
AckTime, "This is your Message Box", 0)
Case 1, -1
Exit Sub
End Select
End Sub

Keep in mind that the first time code compiles, it can take a few extra moments, so on the first execution of this macro, it will take longer than 3 seconds for 3 seconds to elapse. Run the code twice and see if the second time around is closer to 3 seconds than the first execution, which it was for me.
17 years later... ;)
That box does not go away by itself after many seconds have passed and running the code more than once.
Is there maybe something that needs to be added in Tools/References?
 
Upvote 0
I tried the above method to create a timed MsgBox with and without the Select . . Case statement and the PopUp does go away sometimes, and sometimes not. However I did find the following code that appears to work all the time for making a disappearing MsgBox every time you try it:

VBA Code:
Private Sub subClosingPopUp(PauseTime As Integer, Message As String, Title As String)

    Dim WScriptShell As Object
    Dim ConfigString As String
    
    Set WScriptShell = CreateObject("WScript.Shell")
    ConfigString = "mshta.exe vbscript:close(CreateObject(""WScript.Shell"").Popup(""" & Message & """," & PauseTime & ",""" & Title & """))"
    
    WScriptShell.Run ConfigString

End Sub

Private Sub cmdMsg_Click()
    subClosingPopUp 7, "Message body", "The Title"
End Sub
 
Upvote 0
I tried the above method to create a timed MsgBox with and without the Select . . Case statement and the PopUp does go away sometimes, and sometimes not. However I did find the following code that appears to work all the time for making a disappearing MsgBox every time you try it:
For consistent results you can use this alternative using the win32 api
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,435
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top