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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40631009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:32:47  来源:igfitidea点击:

Display popup for a time period in Excel

excelvbapopupmsgbox

提问by Daneel Olivaw

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

我正在尝试生成一个WaitTime在几秒钟内关闭的弹出窗口。

I consulted thislink and thislink.

我咨询了这个链接和这个链接。

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

我尝试应用“ VBA Excel 宏消息框自动关闭”中的方法;我的代码如下:

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

编辑 #1

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

基于@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

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

但是,这并不能解决原来的问题,弹出窗口在 1 秒后不会关闭。

Edit #2

编辑 #2

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

这是@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

回答by Robert J.

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

另一种方法(如果您根本不起作用)。

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

创建一个名为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

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

回答by Daneel Olivaw

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

我终于找到了一个非常简单的解决方案 - 归功于@Orphid,请在以下线程中查看他的回答。

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.

这工作得很好。

回答by Glitch_Doctor

You're just missing the Select Case:

你只是缺少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...

我测试了,它有效......

回答by Akshay Jadhav

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

结束子