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

问题描述 投票:1回答:3

我试图在Excel VBA中生成一个弹出窗口,该弹出窗口在给定的WaitTime后会在几秒钟内自动关闭。我咨询了线程“VBA Excel macro message box auto close”以及thisthis链接。我试图从引用的StackExchange线程中应用该方法;我的代码如下:

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秒后关闭。

有没有人发现我做错了什么?


编辑#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
excel-vba popup msgbox vba excel
3个回答
2
投票

另一种方法(如果你根本不工作)。

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

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

1
投票

我终于找到了一个非常简单的解决方案 - @Orphid的信用,请参阅下面的thread中的答案。

我没有解决与原始代码相关的具体问题,但我设法创建了一个在指定的时间段后关闭的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

这很好用。


0
投票

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

我测试了它的工作原理......

© www.soinside.com 2019 - 2024. All rights reserved.