Auto closing a message box using VBA code

Cingetor

New Member
Joined
Apr 25, 2017
Messages
15
I'm sure this has come up before but I'm having a hard time getting a warning message box to pop up and then close after 10 seconds. I have a 30 minute timer which warns the user after 25 minutes and then closes the workbook after 30 minutes. The code I'm using is:

CreateObject("WScript.Shell").Popup "This workbook has been open for 25 minutes. You have 5 minutes to save your work before Excel closes.", 10, "Warning"

Which, if I put it in a sub and then run the sub, it works fine. But if I call the sub from another sub the message stays open and stops execution of the remaining code until I click the OK button - it does not close after 10 seconds. If I put the command inside the timer sub the same thing happens. I'm not sure what's going on here. Anyone have any ideas on how I can correct this? I need two boxes to open for 10 seconds and then close, one at a 5 minute warning (after 25 minutes) and one saying Excel is closing in 10 seconds.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
'The first part
#If Win64 Then '64?
    Private Declare PtrSafe Function MsgBoxTimeout _
        Lib "user32" _
        Alias "MessageBoxTimeoutA" ( _
            ByVal hwnd As LongPtr, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As VbMsgBoxStyle, _
            ByVal wlange As Long, _
            ByVal dwTimeout As Long) _
    As Long
#Else
    Private Declare Function MsgBoxTimeout _
        Lib "user32" _
        Alias "MessageBoxTimeoutA" ( _
            ByVal hwnd As Long, _
            ByVal lpText As String, _
            ByVal lpCaption As String, _
            ByVal wType As VbMsgBoxStyle, _
            ByVal wlange As Long, _
            ByVal dwTimeout As Long) _
    As Long
#End If
'The second part
Sub btnMsgbox()
    Call MsgBoxTimeout(0, "This message box will be closed after 4 seconds ", "Auto Close MsgBox", vbInformation, 0, 4000)
End Sub




Sub MessageBoxTimer()
    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
 
Upvote 0

Forum statistics

Threads
1,223,838
Messages
6,174,942
Members
452,593
Latest member
Jason5710

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