Option Explicit
Private WithEvents xlApp As Application
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongPtrA" (ByVal hwnd As LongLong, ByVal nIndex As Long) As LongLong
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongLong, ByVal nIndex As Long, ByVal dwNewLong As LongLong) As LongLong
#Else
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUf As LongPtr) As Long
Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Private hwnd As LongPtr
#Else
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hUf As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private hwnd As Long
#End If
Private Sub UserForm_Initialize()
Call AddMinMaxButtons(Form:=Me, MinButton:=True, MaxButton:=True)
End Sub
Private Sub UserForm_Resize()
Dim oWnd As Window
If IsIconic(hwnd) Then
For Each oWnd In Application.Windows
oWnd.WindowState = xlMinimized
Next oWnd
End If
End Sub
Private Sub xlApp_WindowResize(ByVal Wb As Workbook, ByVal Wn As Window)
#If Win64 Then
Const NULL_PTR = 0^
#Else
Const NULL_PTR = 0&
#End If
Const WM_SYSCOMMAND As Long = &H112, SC_RESTORE = &HF120&
If Wn.WindowState <> xlMinimized Then
If IsIconic(hwnd) Then
Call SendMessage(hwnd, WM_SYSCOMMAND, SC_RESTORE, ByVal NULL_PTR)
End If
End If
End Sub
Private Sub AddMinMaxButtons( _
ByVal Form As UserForm, _
Optional ByVal MinButton As Boolean = True, _
Optional ByVal MaxButton As Boolean = True _
)
Const GWL_STYLE = (-16), WS_MINIMIZEBOX = &H20000, WS_MAXIMIZEBOX = &H10000
Call IUnknown_GetWindow(Form, VarPtr(hwnd))
Set xlApp = Application
Call SetWindowLong(hwnd, GWL_STYLE, _
GetWindowLong(hwnd, GWL_STYLE) Or (WS_MINIMIZEBOX * -MinButton) _
Or (WS_MAXIMIZEBOX * -MaxButton))
End Sub