1

I need to combine a topic I found on stackoverflow and Mouse scroll in UserForm.

For my UserForm I activate (hook) the scroll wheel and deactivate (unhook) the scroll wheel when I close the Userform.

For the next update of the application, colleagues want a minimize button. I have achieved this through the following topic; Minimize userform and create icon on taskbar

But now when I minimize the Userform, the scroll is not unhooked (UnhookFormScroll)

I did look into a solution where I use UserForm_Layout() combined with IsIconic (Link how that should be used, but instead I used UnhookFormScroll);

'The 5 lines of code beneath need to be somewhere else in the module of the 
'form
Option Explicit
Private Declare Function IsZoomed Lib "user32" _
  (ByVal hWnd As Long) As Long
Private Declare Function IsIconic Lib "user32" _
  (ByVal hWnd As Long) As Long

Private Sub UserForm_Layout()
'I have edited the code so it works for whoever passes this post in the future

    Dim hWnd as Long

    If IsIconic(hwnd) Then
        UnhookFormScroll
    End if

    If IsZoomed(hwnd) Then
        HookFormScroll Me
    End If

End Sub

This results in a error where Excel/VBA doesn't recognize the variable "hwnd"

Community
  • 1
  • 1
Joey b
  • 25
  • 8
  • In the link you included for `IsIconic`, the code includes this line `Private Declare Function IsIconic Lib "user32" Alias "IsIconic" (ByVal hwnd As Long) As Long`, in the module in which this is used. I don't see it in your code, but I'm pretty sure it's required. – Mistella Sep 25 '18 at 14:11
  • I have included this in the module, but on top of all the code along with all the other "Private Declarations" I have used to minimize the UserForm – Joey b Sep 25 '18 at 14:14
  • 1
    It looks like in the actual function, they're using `hWnd` (with capitalized W). Another page I found used `Userform.hWnd`. Have you tried either of those? – Mistella Sep 25 '18 at 14:45
  • The trick was to add `Dim hWnd as Long` to the code, and it works brilliantly now. When I minimize I can scroll where ever I want and when I open the UserForm through the taskbar again I can scroll through the UserForm again. Now I need to find something for when I go outside my UserForm, but that is something for tomorrow. Thanks for your help! – Joey b Sep 25 '18 at 14:55

0 Answers0