Option Explicit
Sub test()
Dim aResponseText As Variant
Dim i As Long
aResponseText = Array("No Response", "OK", "Cancel", "Abort", "Retry", "Ignore", "Yes", "No")
i = MsgBoxTimed("Do not touch -- Three Seconds", "Test Timed Msgbox", vbExclamation, vbOKOnly, , 3)
MsgBox "Returned from function -- You can click now = " & aResponseText(i)
i = MsgBoxTimed("Do not touch -- 'Yes' in Three Seconds - ", "Test Timed Msgbox (Y/N)", vbCritical, vbYesNo, vbDefaultButton1, 3)
MsgBox "Returned from function -- You can click now = " & aResponseText(i)
i = MsgBoxTimed("Do not touch -- 'No' in Three Seconds - ", "Test Timed Msgbox (Y/N)", vbCritical, vbYesNo, vbDefaultButton2, 3)
MsgBox "Returned from function -- You can click now = " & aResponseText(i)
i = MsgBoxTimed("User override default -- 'Click 'Yes' within 10 seconds, otherwise default = 'No'", "Test Timed Msgbox (Y/N)", vbCritical, vbYesNo, vbDefaultButton2, 10)
MsgBox "Returned from function -- You can click now = " & aResponseText(i)
i = MsgBoxTimed("Waiting for user input", "Test Timed Msgbox", vbExclamation, , , 0)
MsgBox "Returned from function -- You can click now = " & aResponseText(i)
End Sub
Function MsgBoxTimed(sMsg As String, sTitle As String, _
Optional MsgIcon As VbMsgBoxStyle = vbInformation, _
Optional MsgButtons As VbMsgBoxStyle = vbOKOnly, _
Optional MsgButtonsDefault As VbMsgBoxStyle = vbDefaultButton1, _
Optional SecDelay As Long = 0) _
As VbMsgBoxResult
Const vbNoResponse As Long = -1
Dim iResponse As VbMsgBoxResult, iDefault As VbMsgBoxResult
Dim iWait As Long
Dim objShell As Object
Select Case MsgButtons
Case vbOKOnly
iDefault = vbOK
Case vbOKCancel
If MsgButtonsDefault = vbDefaultButton1 Then
iDefault = vbOK
Else
iDefault = vbCancel
End If
Case vbAbortRetryIgnore
If MsgButtonsDefault = vbDefaultButton1 Then
iDefault = vbAbort
ElseIf MsgButtonsDefault = vbDefaultButton2 Then
iDefault = vbRetry
Else
iDefault = vbIgnore
End If
Case vbYesNoCancel
If MsgButtonsDefault = vbDefaultButton1 Then
iDefault = vbYes
ElseIf MsgButtonsDefault = vbDefaultButton2 Then
iDefault = vbNo
Else
iDefault = vbCancel
End If
Case vbYesNo
If MsgButtonsDefault = vbDefaultButton1 Then
iDefault = vbYes
Else
iDefault = vbNo
End If
Case vbRetryCancel
If MsgButtonsDefault = vbDefaultButton1 Then
iDefault = vbRetry
Else
iDefault = vbCancel
End If
End Select
'display the box, and return the value
Set objShell = CreateObject("WScript.Shell")
DoEvents
iResponse = objShell.Popup(sMsg, SecDelay, sTitle, _
MsgIcon + MsgButtons + MsgButtonsDefault + vbSystemModal)
Set objShell = Nothing
If iResponse = vbNoResponse Then
MsgBoxTimed = iDefault
Else
MsgBoxTimed = iResponse
End If
End Function