-
Oct 17th, 2009, 03:45 PM
#1
[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
Last edited by RobDog888; Oct 17th, 2009 at 03:55 PM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Oct 17th, 2009, 04:09 PM
#2
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Jun 25th, 2020, 01:18 AM
#3
New Member
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
-
Dec 11th, 2020, 08:03 AM
#4
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
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.
Along with the sunshine there has to be a little rain sometime.
-
Dec 11th, 2020, 12:11 PM
#5
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
-
Dec 11th, 2020, 12:14 PM
#6
Re: [FAQ's: OD] How do I set a UserForm to be the TopMost form throughout Windows?
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|