As I open a UserForm, I want to maximize the size of the form
(making it as large as my monitor).
If you don't mind that your user can move the form around after it is
maximized, then maybe you can use this code solution.
The following is pieced together from a lot of different API sources I found
on Google... I am confident that I have everything correct and that this
will work correctly in your application (as with any test code provided in a
newsgroup response, you should try this out on a copy of your workbook
first). What the code does is make your UserForm fill the entire screen
(which is what I assume you meant when you said "making it as large as my
monitor"). Put all the code below my signature into a Standard Module (click
Insert/Module from the VBA editor's menu, copy/paste the code below my
signature into the window that appears). Then, to maximize the form, execute
this statement...
MaximizeUserForm Me.Caption
from anywhere within your UserForm's code (since that is where Me exist at;
use the actual UserForm's name in place of Me for code outside of the
UserForm) WITH THE EXCEPTION of the UserForm's Initialize event (it will
produce a "Form already displayed; can't show modally" error if you try it
there). For your indicated usage, you will probably want to execute the
above statement from the UserForm's Activate event.
Rick
'******** Place all of the following into a Standard Module ********
Private Declare Function GetDesktopWindow Lib "user32" () 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 Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, _
ByRef lpdwProcessId As Long) As Long
Private Declare Function ShowWindow Lib "user32" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Private Const SW_MAXIMIZE = 3
Private Function FindOurWindow(Optional sClass As String = vbNullString, _
Optional sCaption As String = vbNullString)
Dim hWndDesktop As Long
Dim hWnd As Long
Dim hProcThis As Long
Dim hProcWindow As Long
hProcThis = GetCurrentProcessId
hWndDesktop = GetDesktopWindow
Do
hWnd = FindWindowEx(hWndDesktop, hWnd, sClass, sCaption)
GetWindowThreadProcessId hWnd, hProcWindow
Loop Until hProcWindow = hProcThis Or hWnd = 0
FindOurWindow = hWnd
End Function
Public Sub MaximizeUserForm(UFcaption As String)
Dim UF_hWnd As Long
UF_hWnd = FindOurWindow(, UFcaption)
ShowWindow UF_hWnd, SW_MAXIMIZE
End Sub