方法一:官方,失败
Sub MessageBoxTimer()Dim AckTime As Integer, InfoBox As ObjectSet InfoBox = CreateObject("WScript.Shell")'Set the message box to close after 10 secondsAckTime = 10Select Case InfoBox.Popup("Click OK (this window closes automatically after 10 seconds).", _AckTime, "This is your Message Box", 0)Case 1, -1Exit SubEnd SelectEnd SubSub Sample()Dim WSH As ObjectSet WSH = CreateObject("WScript.Shell")WSH.Popup "消息内容",5,"标题",vbInformationSet WSH = NothingEnd 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 LongPublic Declare Function KillTimer Lib "user32" ( _ByVal hWnd As Long, _ByVal nIDEvent As Long) As LongDim TID As LongSub MessageBoxTimer(message,sleep)TID = SetTimer(0, 0, sleep*1000, AddressOf CloseTest)MsgBox messageEnd SubSub CloseTest(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idevent As Long, _ByVal Systime As Long)Application.SendKeys "~", TrueKillTimer 0, TIDEnd Sub
代码解析:
第1行代码到第9行代码是API函数声明。
Test过程显示一个消息框并在3秒钟后运行CloseTest过程。
CloseTest过程发送一个确定键给Excel程序关闭显示的消息框。
运行下面过程显示一个如图 742所示的消息框并在2秒钟后关闭。
Sub MessageBoxTimer(message,2)
