方法一:官方,失败

    1. Sub MessageBoxTimer()
    2. Dim AckTime As Integer, InfoBox As Object
    3. Set InfoBox = CreateObject("WScript.Shell")
    4. 'Set the message box to close after 10 seconds
    5. AckTime = 10
    6. Select Case InfoBox.Popup("Click OK (this window closes automatically after 10 seconds).", _
    7. AckTime, "This is your Message Box", 0)
    8. Case 1, -1
    9. Exit Sub
    10. End Select
    11. End Sub
    12. Sub Sample()
    13. Dim WSH As Object
    14. Set WSH = CreateObject("WScript.Shell")
    15. WSH.Popup "消息内容",5,"标题",vbInformation
    16. Set WSH = Nothing
    17. End Sub

    方法二:API方式,成功

    1. Public Declare Function SetTimer Lib "user32" ( _
    2. ByVal hWnd As Long, _
    3. ByVal nIDEvent As Long, _
    4. ByVal uElaspe As Long, _
    5. ByVal lpTimerFunc As Long) As Long
    6. Public Declare Function KillTimer Lib "user32" ( _
    7. ByVal hWnd As Long, _
    8. ByVal nIDEvent As Long) As Long
    9. Dim TID As Long
    10. Sub MessageBoxTimer(message,sleep)
    11. TID = SetTimer(0, 0, sleep*1000, AddressOf CloseTest)
    12. MsgBox message
    13. End Sub
    14. Sub CloseTest(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idevent As Long, _
    15. ByVal Systime As Long)
    16. Application.SendKeys "~", True
    17. KillTimer 0, TID
    18. End Sub

    代码解析:
    第1行代码到第9行代码是API函数声明。
    Test过程显示一个消息框并在3秒钟后运行CloseTest过程。
    CloseTest过程发送一个确定键给Excel程序关闭显示的消息框。
    运行下面过程显示一个如图 742所示的消息框并在2秒钟后关闭。
    Sub MessageBoxTimer(message,2)