Display popup for a time period in Excel

前端 未结 5 1277
萌比男神i
萌比男神i 2021-01-23 17:41

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

相关标签:
5条回答
  • 2021-01-23 18:24

    The following code works for me:

    Sub TimeBasedPopUp()
    
    Dim WaitTime As Integer
    
    WaitTime = 1
    Select Case CreateObject("WScript.Shell").Popup("The message box will close in 1 second.",_
    WaitTime, "MS Excel")
    Case 1, -1
    

    End Select

    End Sub

    0 讨论(0)
  • 2021-01-23 18:32

    Below code work for me, I added a 2-sec delay before the popup message appears. After 4-sec it auto disappear. I learn it from Mr. Dinesh Kumar Takyar. He added a 5-sec delay b4 popup appears. His youtube link https://www.youtube.com/watch?v=x1nmqVRrq-Q&list=PLwC8syx0i_6nHjAogOm9m4oGBq40YHkXV&index=4 I think the key issue is you need a delay for the popup timer to work. Maybe the Excel application needs to run for a while b4 the popup appears.


    Option Explicit
    
    Const PopUpTime As Integer = 4
    
    Sub ShellMessageBox()
    
    Dim MsgBoxWithTimer As Integer
    
    MsgBoxWithTimer=CreateObject("WScript.Shell").Popup("Put your message here", PopUpTime, _
    "Notice!", 0)
    
    End Sub
    
    Sub startTimer()
    Application.OnTime Now + TimeValue("00:00:02"), "ShellMessageBox"
    End Sub
    
    Private Sub Workbook_Open()
    
    startTimer
    
    End Sub
    
    0 讨论(0)
  • 2021-01-23 18:42

    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.

    0 讨论(0)
  • 2021-01-23 18:43

    Another approach (if your would not work at all).

    Create a new userform named frm_Popup and add a label there named lbl_Message. Add the following void to userform code:

    Public Sub StartProcess(iTime As Integer)
        Me.lbl_Message.Caption = "The message box will close in " & iTime & " second(s)."
    End Sub
    

    then in your module:

    Sub ShowMessage()
        Dim iTimeToWait As Integer
            iTimeToWait = 2
    
        With frm_Popup
            .Show False
            Call .StartProcess(iTimeToWait)
        End With
    
        Application.OnTime Now + TimeValue("00:00:" & iTimeToWait), "HidePopup"
    End Sub
    
    Private Sub HidePopup()
        Unload frm_Popup
    End Sub
    
    0 讨论(0)
  • 2021-01-23 18:43

    You're just missing the Select Case:

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

    I tested and it works...

    0 讨论(0)
提交回复
热议问题