Created
December 24, 2025 18:19
-
-
Save belst-n/9bab2a7ea3f265e0440ddcf6184978dd to your computer and use it in GitHub Desktop.
VBA code for Office to monitor and automatically dismiss MessageBox windows that appear in Excel. Monitoring is performed from a separate Word document process.I did not write this code. The code was authored originally by A.S.H. and was copied from https://stackoverflow.com/a/41826510/22859713.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' Author: A.S.H; Source: https://stackoverflow.com/a/41826510/22859713 | |
| ' Excel - Class Module VBA | |
| Private killerDoc As Object | |
| Private Sub Class_Initialize() | |
| On Error Resume Next | |
| Set killerDoc = CreateObject("Word.Application").Documents.Open(Filename:="C:\BElston\Office\Custom\MessageBoxMonitor.docm", ReadOnly:=True) | |
| If Err.Number <> 0 Then | |
| If Not killerDoc Is Nothing Then killerDoc.Close False | |
| Set killerDoc = Nothing | |
| MsgBox "could not lauch The mboxKiller killer. The message-box shall be closed manuallt by the user." | |
| End If | |
| End Sub | |
| Private Sub Class_Terminate() | |
| On Error Resume Next | |
| If Not killerDoc Is Nothing Then killerDoc.Application.Quit False | |
| End Sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' Author: A.S.H; Source: https://stackoverflow.com/a/41826510/22859713 | |
| ' Test MessageBoxMonitor | |
| Sub Excel_MessageBoxMonitorTest() | |
| Dim killer: Set killer = New mboxKiller | |
| Excel_MessageBoxMonitorTest_Simulate | |
| Excel_MessageBoxMonitorTest_Simulate | |
| Excel_MessageBoxMonitorTest_Simulate | |
| End Sub | |
| Private Sub Excel_MessageBoxMonitorTest_Simulate() | |
| Dim i As Long | |
| For i = 0 To 1000: DoEvents: Next | |
| MsgBox "This is a message box to simulate the message box of the addin." & VbCrLf & _ | |
| "It will be automatically closed by the Word app mboxKiller" | |
| End Sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' Author: A.S.H; Source: https://stackoverflow.com/a/41826510/22859713 | |
| ' Word - ThisDocument VBA | |
| Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long | |
| Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long | |
| Public Sub WaitAndKillWindow() | |
| On Error Resume Next | |
| Dim h As Long: h = FindWindow(vbNullString, "Microsoft Excel") | |
| If h <> 0 Then SendMessage h, 16, 0, 0 ' <-- WM_Close | |
| Application.OnTime Now + TimeSerial(0, 0, 1), "WaitAndKillWindow" | |
| End Sub | |
| Private Sub Document_Open() | |
| WaitAndKillWindow | |
| End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment