Search code examples
excelvbapopupmsgbox

Display popup for a time period in Excel


I am trying to generate a popup that closes after a given WaitTime in seconds.

I consulted this link and this link.

I tried to apply the method from "VBA Excel macro message box auto close"; my code is the following:

Sub TestSubroutine()

Dim TemporalBox As Integer
Dim WaitTime As Integer
Dim WScriptShell As Object

Set WScriptShell = CreateObject("WScript.Shell")

WaitTime = 1
TemporalBox = WScriptShell.Popup("The message box will close in 1 second.", _
WaitTime, "File processed")

End Sub

The popup is displayed but it never closes after one second.


Edit #1

Based on @Skip Intro comment, I have updated the code:

Sub TestSubroutine()

Dim WaitTime As Integer

WaitTime = 1
CreateObject("WScript.Shell").Popup "The message box will close in 1 second.", _
WaitTime, "File processed"

End Sub

However this does not solve the original issue, the popup does not close after 1 second.

Edit #2

This is the code suggested by @Glitch_Doctor, however it still doesn't work:

Sub TestSubroutine()

Dim TemporalBox As Integer
Dim WaitTime As Integer
Dim WScriptShell As Object
Dim test

Set WScriptShell = CreateObject("WScript.Shell")

WaitTime = 1
Select Case TemporalBox = WScriptShell.Popup("The message box will close in 1 second.", _
WaitTime, "File processed")
    Case 1, -1
End Select

End Sub

Solution

  • I finally found a very simple solution - credits to @Orphid, see his answer in the following thread.

    I did not solve the specific issue related to my original code, but I managed to create a PopUp that closes after a specified period of time. The code is the following:

    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
    

    This works just fine.