在 Excel 的时间段内显示弹出窗口

Display popup for a time period in Excel

我正在尝试生成一个在给定 WaitTime 秒后关闭的弹出窗口。

我咨询了this link and thislink.

我尝试应用“VBA Excel macro message box auto close”中的方法;我的代码如下:

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

显示弹出窗口,但一秒钟后它永远不会关闭。


编辑#1

根据@Skip Intro 的评论,我更新了代码:

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

但这并没有解决原来的问题,弹出窗口在 1 秒后没有关闭。

编辑 #2

这是@Glitch_Doctor建议的代码,但它仍然不起作用:

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

你只是错过了 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

我测试过并且有效...

另一种方法(如果你根本行不通)。

创建一个名为 frm_Popup 的新用户表单,并在其中添加一个名为 lbl_Message 的标签。将以下 void 添加到用户窗体代码:

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

然后在您的模块中:

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

我终于找到了一个非常简单的解决方案——归功于@Orphid,请在下面查看他的回答

我没有解决与我的原始代码相关的具体问题,但我设法创建了一个在指定时间段后关闭的 PopUp。代码如下:

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

这很好用。

以下代码适用于我:

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

结束Select

结束子

下面的代码对我有用,我在弹出消息出现之前添加了 2-sec 延迟。 4-sec 后自动消失。我是从 Dinesh Kumar Takyar 先生那里学来的。他添加了一个 5-sec 延迟 b4 弹出窗口出现。他的 YouTube link https://www.youtube.com/watch?v=x1nmqVRrq-Q&list=PLwC8syx0i_6nHjAogOm9m4oGBq40YHkXV&index=4 我认为关键问题是您需要延迟弹出计时器才能工作。也许 Excel 应用程序需要 运行 一段时间 b4 弹出窗口出现。


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