VB and VBA Users Source Code: Preventing any Msgbox's from being display (and capturing the message)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Preventing any Msgbox's from being display (and capturing the message)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, September 11, 2002
Hits:
1284
Category:
Windows API
Article:
Occasionally it is useful to stop any Msgbox's from being displayed in your application. The following code allows you to turn off all Msgbox's in your application and capture their contents. Option Explicit Private Declare Function GetForegroundWindow Lib "user32" () As Long Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal zlhHook As Long) As Long 'Purpose : To capture any Msgbox's shown on this thread 'Inputs : bEnabled If True switches on msgbox capturing, else switches off. 'Outputs : N/A (see the function zCaptureMsgbox where the contents of the msgbox are printed) 'Author : Andrew Baker 'Date : 11/Sep/2002 13:17 'Notes : PLACE IN A MODULE. Can only be used within the application process. 'Revisions : 'Assumptions : Function MsgboxCapture(bEnabled As Boolean) Const WH_CBT = 5 'Respond to certain system actions, making it possible to develop computer-based training (CBT) for applications (WH_CBT). Dim lThread As Long, lhInst As Long Static slhHook As Long If bEnabled = False Then lThread = GetCurrentThreadId slhHook = SetWindowsHookEx(WH_CBT, AddressOf zCaptureMsgbox, App.hInstance, lThread) Else 'Release the CBT hook UnhookWindowsHookEx slhHook End If End Function 'Private call back function used to capture and close and MsgBox's 'Notes : PLACE IN A MODULE Private Function zCaptureMsgbox(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Const WM_CLOSE = &H10&, HCBT_ACTIVATE = 5 Const clMaxText As Long = 255 Const WH_MSGFILTER = (-1) Dim sClassName As String * clMaxText, sMsgboxText As String * clMaxText Dim lHwndWindow As Long, lNumChars As Long, lHwndText As Long If lMsg = 6 Then lHwndWindow = GetForegroundWindow lNumChars = clMaxText lNumChars = GetClassName(lHwndWindow, sClassName, lNumChars) If UCase(Left$(sClassName, lNumChars)) = "#32770" Then 'The active window is a Msgbox, get the message and close it lHwndText = FindWindowEx(lHwndWindow, 0&, "Static", vbNullString) lNumChars = clMaxText lNumChars = GetWindowText(lHwndText, sMsgboxText, lNumChars) Debug.Print "Message box captured: " & Left$(sMsgboxText, lNumChars) Call SendMessage(lHwndWindow, WM_CLOSE, 0&, 0&) End If End If zCaptureMsgbox = 0 End Function 'Demonstration code (PLACE IN A FORM) Private Sub Command1_Click() MsgboxCapture False MsgBox "Hello Doesn't show" MsgboxCapture True MsgBox "Hello Does show" End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder