Creating a general minimization on the user form

mmn1000

Board Regular
Joined
Mar 17, 2020
Messages
80
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello dear friends, don't be bored
I was looking for an article about creating a minimizer on the Excel user form, so that by clicking on it, the whole Excel will be minimized along with the active user form, and when I click on the minimized Excel icon in the taskbar, both Excel and the user form will be minimized again. Actively return to the initial state.
Thank you dear professors
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Good day

Create a command button on your userform and assign a minimise picture to it. Then Use code below for command button click event.

VBA Code:
Private Sub CommandButton1_Click()
     ActiveWindow.WindowState = xlMinimized
End Sub

Screenshot 2023-02-22 112636.png
 
Upvote 0
Good day

Create a command button on your userform and assign a minimise picture to it. Then Use code below for command button click event.

VBA Code:
Private Sub CommandButton1_Click()
     ActiveWindow.WindowState = xlMinimized
End Sub

View attachment 85932
Thank you for your guidance
I use the following code for my userform

VBA Code:
Private Sub UserForm_Activate()
'Set the min/max button setting for the form.
 AddMinMaxButtons Me.Caption, MinButton:=True, MaxButton:=True
End Sub

In this code, only the user form is minimized
How can I change this code to work like your code?
 

Attachments

  • 45.png
    45.png
    4.8 KB · Views: 9
Upvote 0
VBA Code:
Application.WindowState = xlMinimized
This code of yours is like the Jimmypop code, I need to put a button for it
Look at the code that I sent. I want to do this with the userform itself
 
Upvote 0
This code of yours is like the Jimmypop code, I need to put a button for it
Look at the code that I sent. I want to do this with the userform itself
You didn't post your entire code.
The AddMinMaxButtons routine is missing... Show us the code in the AddMinMaxButtons routine.
 
Upvote 0
Here is an example that shows how to minimize all excel opened application\workbook windows along with the userform.

Workbook Demo:
MinimizeFormAndExcel.xlsm


Place this code in the UserForm Module:
VBA Code:
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
 
Last edited:
Upvote 1
Solution
Here is an example that shows how to minimize all excel opened application\workbook windows along with the userform.

Hi Jaafar... Tested now and works on my side... Also saw the AddMinMaxButtons routine was missing but have been a bit busy to reply to the OP... thanks for assistance😎
 
Upvote 0
Here is an example that shows how to minimize all excel opened application\workbook windows along with the userform.

Workbook Demo:
MinimizeFormAndExcel.xlsm


Place this code in the UserForm Module:
VBA Code:
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
Thank you for your time
that was perfect
Thank You
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top