ExcelVBA 指定時間でダイアログを閉じる

検索上位ででてくるWSH使うのは安定しないから使わないほうが良い

Private Declare Function MessageBoxTimeoutA Lib "user32" ( _
                                            ByVal hWnd As Long, _
                                            ByVal lpText As String, _
                                            ByVal lpCaption As String, _
                                            ByVal uType As Long, _
                                            ByVal wLanguageId As Long, _
                                            ByVal dwMilliseconds As Long) As Long

...

Function TimeoutMsgBox(aStr As String, aTitle As String, aTimeouts As Long, aNType As Long) As Long

    Dim AnswerVal As Long

    AnswerVal = MessageBoxTimeoutA(0, aStr, aTitle, aNType, 0, aTimeouts * 1000)
    
    TimeoutMsgBox = AnswerVal

End Function

Sub テスト()
    If TimeoutMsgBox("5秒以内にキャンセルしないと自動でブックを閉じます", "確認", 5, vbOKCancel) <> vbCancel Then
        'ブック閉じる
        ActiveWorkbook.Close SaveChanges:=True
    End If
End Sub

ネタ元

takeichi.work