Option Explicit
Public oTarget As Range
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 CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" _
() As Long
Private Declare Function GetClassName Lib "user32.dll" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" _
Alias "GetWindowTextA" _
(ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) As Long
Private Declare Function FindWindowEx Lib "user32.dll" _
Alias "FindWindowExA" (ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Private Const WH_CBT As Long = 5
Private Const GWL_HINSTANCE As Long = (-6)
Private Const HCBT_ACTIVATE = 5
Private lCBTHook As Long
'===================='
' Public routines. '
'===================='
Public Sub SetHook()
lCBTHook = SetWindowsHookEx(WH_CBT, AddressOf CBTProc, _
GetAppInstance, GetCurrentThreadId)
End Sub
'===================='
' Private routines. '
'===================='
Private Function CBTProc _
(ByVal idHook As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim sBuffer As String
Dim lRetVal As Long
Dim lStaticHwnd As Long
Select Case idHook
Case Is = HCBT_ACTIVATE
' Some Wnd has been activated within the excel process.
sBuffer = Space(256)
lRetVal = GetClassName(wParam, sBuffer, 256)
'Is it our 'MsgBox' wnd ?
If Left(sBuffer, lRetVal) = "#32770" Then
'if so retrieve the Static window handle.
lStaticHwnd = FindWindowEx(wParam, 0, "Static", vbNullString)
'retrieve the Static text.
sBuffer = Space(256)
lRetVal = GetWindowText(lStaticHwnd, sBuffer, 256)
'log the Msgbox text-time and key strokes.
Call LogEntry(Left(sBuffer, lRetVal))
End If
'done with hook.
UnhookWindowsHookEx lCBTHook
End Select
'Call next hook if any.
CBTProc = CallNextHookEx _
(lCBTHook, idHook, ByVal wParam, ByVal lParam)
End Function
Private Function GetAppInstance() As Long
GetAppInstance = GetWindowLong _
(FindWindow("XLMAIN", Application.Caption), GWL_HINSTANCE)
End Function
Private Sub LogEntry(ByVal Message As String)
Application.EnableEvents = False
Cells(Rows.Count, 1).End(xlUp).Offset(1) = Message
Cells(Rows.Count, 1).End(xlUp).Offset(, 1) = Format(Now, "hh:mm:ss")
Cells(Rows.Count, 1).End(xlUp).Offset(, 2) = oTarget
Application.EnableEvents = True
End Sub