Option Explicit
Private Type POINTAPI
x As Long
Y As Long
End Type
#If VBA7 Then
Private Type MSG
hwnd As LongPtr
message As Long
wParam As LongPtr
lParam As LongPtr
time As Long
pt As POINTAPI
End Type
Private Declare PtrSafe Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As LongPtr, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare PtrSafe Function WaitMessage Lib "user32" () As Long
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Private XlDeskHwnd As LongPtr
Private WbkHwnd As LongPtr
#Else
Private Type MSG
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Private Declare Function WaitMessage Lib "user32" () As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal hwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam 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 XlDeskHwnd As Long
Private WbkHwnd As Long
#End If
Private Const WM_MOUSEWHEEL = &H20A
Private Const PM_REMOVE = &H1
Private bCancel As Boolean
Private Sub Workbook_Open()
Dim tMSG As MSG
Dim bIsCompatibilityMode As Boolean
Dim sExcel8CompatibilityModeCaption As String
If Val(Application.Version) >= 12 Then ' >= 2007
bIsCompatibilityMode = CallByName(Me, "Excel8CompatibilityMode", VbGet)
If bIsCompatibilityMode Then
If Application.International(xlCountryCode) = 33 Then 'French
sExcel8CompatibilityModeCaption = " [Mode de compatibilité]"
Else 'English
sExcel8CompatibilityModeCaption = " [Compatibility Mode]"
End If
End If
End If
XlDeskHwnd = FindWindowEx(Application.hwnd, 0, "XLDESK", vbNullString)
WbkHwnd = FindWindowEx(XlDeskHwnd, 0, "EXCEL7", Me.Name & sExcel8CompatibilityModeCaption)
If WbkHwnd = 0 Then WbkHwnd = FindWindowEx(XlDeskHwnd, 0, "EXCEL7", Me.Name & " " & sExcel8CompatibilityModeCaption)
If WbkHwnd Then
Do
WaitMessage
If PeekMessage(tMSG, WbkHwnd, 0, 0, PM_REMOVE) Then
Select Case tMSG.message
Case WM_MOUSEWHEEL
If tMSG.wParam > 0 Then
ActiveWindow.SmallScroll up:=1
Else
ActiveWindow.SmallScroll down:=1
End If
Case Else
PostMessage tMSG.hwnd, tMSG.message, tMSG.wParam, tMSG.lParam
End Select
End If
DoEvents
Loop Until bCancel
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
bCancel = True
End Sub