Problem
Call the procedure when the Excel main window changes.
First try:
Sub Workbook_WindowResize(ByVal Wn As Window) Debug.Print Wn.Width & "x" & Wn.Height End Sub
Results:
The subroutine is called whenever the inner window of the workbook changes, but not when the size of the application window changes.
Second attempt
Dim WithEvents App As Application Private Sub App_WindowResize(ByVal Wb As Workbook, ByVal Wn As Window) Debug.Print Wn.Width & "x" & Wn.Height End Sub
Results:
Oddly enough, the same thing happens that happened before, which definitely surprised me. The event occurs only when the window size of the workbook is changed instead of the application window.
For this reason, I began to study the use of the window API.
There are many examples of installing SystemWide keyboard and mouse hooks using the window APIs. This happens in the same directions:
Public Enum enHookTypes WH_CALLWNDPROC = 4 WH_CALLWNDPROCRET = 12 WH_CBT = 5 WH_DEBUG = 9 WH_FOREGROUNDIDLE = 11 WH_GETMESSAGE = 3 WH_HARDWARE = 8 WH_JOURNALPLAYBACK = 1 WH_JOURNALRECORD = 0 WH_MOUSE = 7 WH_MSGFILTER = (-1) WH_SHELL = 10 WH_SYSMSGFILTER = 6 WH_KEYBOARD_LL = 13 WH_MOUSE_LL = 14 WH_KEYBOARD = 2 End Enum Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As LongPtr, ByVal lpfn As Long, ByVal hMod As Long, ByVal dwThreadId As Long) As LongPtr Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long Private Declare Function GetLastError Lib "kernel32" () As Long 'Ensure that your hook procedure does not interfere with the normal operation of other hook procedures Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long Public hndl As Long Sub HookWindow() hndl = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf measureWindow, Application.Hinstance, 0&) Debug.Print hndl & "~~" & GetLastError() End Sub Sub unhookWindow() ret = UnhookWindowsHookEx(hndl) Debug.Print ret End Sub Public Sub measureWindow(code As Long, wParam As Long, lParam As Long) If code > 0 Then Debug.Print ThisWorkbook.Windows(1).Width & "x" & ThisWorkbook.Windows(1).Height Else ret = CallNextHookEx(measureWindow, code, wParam, lParam) End If End Sub
Results:
If I replaced WH_CALLWNDPROC with:
hndl = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf measureWindow, Application.Hinstance, 0&)
with WH_KEYBOARD_LL subroutine is called whenever a key is pressed. Similarly, if I replace it with WH_MOUSE_LL , the subroutine is called whenever the mouse moves or the mouse button is pressed.
The problem is that when I try to connect a routine to WH_CALLWNDPROC , nothing happens?
Why?
I'm still not sure, but the same is true for all ENUMS in enHookTypes except WH_MOUSE_LL and WH_KEYBOARD_LL . After reviewing the WinAPI documentation, I read that you can use GetLastError from Kernel32.dll to find out why the operation failed.
The error numbers that I have received so far (in decimal form) are error 5 (for MAGAZINE hooks) and error 1428 for the rest.
In the end, this also failed.