[FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
Using two API calls we can obtain the UserForm window handle which is needed to pass as an argument to another API which changes the UserForms "Z Ordering" position of it.
Add a UserForm to an Office application and a Command button to toggle the UserForm from Topmost to Not Topmost.
Enjoy :)
Code:
Option Explicit
'Written By RobDog888 - VB/Office Guru™
'Add a Command Button so you can toggle the userform's topmost effect
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetWindowPos Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private mlHwnd As Long
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Not Topmost" Then
SetWindowPos mlHwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
CommandButton1.Caption = "Topmost"
Else
SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
CommandButton1.Caption = "Not Topmost"
End If
End Sub
Private Sub UserForm_Initialize()
mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
Do While mlHwnd = 0
mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
DoEvents
Loop
'Set topmost
SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
CommandButton1.Caption = "Not Topmost"
End Sub
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
If you make a small modification in the UserForm_Initialize event you can make your UserForm act like a normal Windows Form like it was independant from the main Excel window.
Using the above code, add one more line of code as shown below...
Code:
Private Sub UserForm_Initialize()
mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
Do While mlHwnd = 0
mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
DoEvents
Loop
'Set topmost
SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
CommandButton1.Caption = "Not Topmost"
'Minimize Excel's main window if you want to present the UserForm as a standard Form not "associated" with Excel.
Application.WindowState = xlMinimized
End Sub
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
Hi RobDog888,
awesome idea!!!
I have a question because i want to do userform as TOP on everything, even if i am opening browser, it is possible ?
Now when i am opening userform as top, net i am swithing on browser - the browser is higher than userform.
Can you please help if you are here still or maybe a anyone else?
Best,
Jacek
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
Quote:
Originally Posted by
RobDog888
If you make a small modification in the UserForm_Initialize event you can make your UserForm act like a normal Windows Form like it was independant from the main Excel window.
Using the above code, add one more line of code as shown below...
Code:
Private Sub UserForm_Initialize()
mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
Do While mlHwnd = 0
mlHwnd = FindWindow("ThunderDFrame", "UserForm1") 'Change to match your userforms caption
DoEvents
Loop
'Set topmost
SetWindowPos mlHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
CommandButton1.Caption = "Not Topmost"
'Minimize Excel's main window if you want to present the UserForm as a standard Form not "associated" with Excel.
Application.WindowState = xlMinimized
End Sub
I get reprimanded if I use DoEvents in any code !
Poppa.
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
Quote:
Originally Posted by
Poppa Mintin
I get reprimanded if I use DoEvents in any code !
Poppa.
Since there is no asynchronous functions in VBA we are left with minimal options. We dont want to lock up the UI while waiting for a userform to appear. It may be UI heavy or such and not pop up quickly. Also, this thread is 11 years old LOL
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
Quote:
Originally Posted by
jaryszek
Hi RobDog888,
awesome idea!!!
I have a question because i want to do userform as TOP on everything, even if i am opening browser, it is possible ?
Now when i am opening userform as top, net i am swithing on browser - the browser is higher than userform.
Can you please help if you are here still or maybe a anyone else?
Best,
Jacek
Yes, there is another API for SetTopMostWindow you can integrate into the code.