方法一:官方,失败
Sub MessageBoxTimer()
Dim AckTime As Integer, InfoBox As Object
Set InfoBox = CreateObject("WScript.Shell")
'Set the message box to close after 10 seconds
AckTime = 10
Select Case InfoBox.Popup("Click OK (this window closes automatically after 10 seconds).", _
AckTime, "This is your Message Box", 0)
Case 1, -1
Exit Sub
End Select
End Sub
Sub Sample()
Dim WSH As Object
Set WSH = CreateObject("WScript.Shell")
WSH.Popup "消息内容",5,"标题",vbInformation
Set WSH = Nothing
End Sub
方法二:API方式,成功
Public Declare Function SetTimer Lib "user32" ( _
ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElaspe As Long, _
ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" ( _
ByVal hWnd As Long, _
ByVal nIDEvent As Long) As Long
Dim TID As Long
Sub MessageBoxTimer(message,sleep)
TID = SetTimer(0, 0, sleep*1000, AddressOf CloseTest)
MsgBox message
End Sub
Sub CloseTest(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idevent As Long, _
ByVal Systime As Long)
Application.SendKeys "~", True
KillTimer 0, TID
End Sub
代码解析:
第1行代码到第9行代码是API函数声明。
Test过程显示一个消息框并在3秒钟后运行CloseTest过程。
CloseTest过程发送一个确定键给Excel程序关闭显示的消息框。
运行下面过程显示一个如图 742所示的消息框并在2秒钟后关闭。
Sub MessageBoxTimer(message,2)