Please help - To keep my excel vb userform on top of all others windows.

Joined
Apr 19, 2014
Messages
3
I was referring to the code in the below link -
http://www.mrexcel.com/forum/excel-...erform-top-all-other-windows.html#post3783183

Above code that you've mentioned is not working! Please help me to get rid of the issue that i'm facing.

I have two user forms A and B; I load userform B when I click a button in userform A and wanted userform B to be on all of the other windows whichever is opened.

Problem: My user form B is not on top of other windows. Its goes behind other windows.

My module 'modFrameOnTop' has the below code:

Code:
Option Explicit

Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Public Const HWND_TOP = 0
Public Const HWND_BOTTOM = 1
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2

Public Declare PtrSafe Function SetWindowPos Lib "user32" _
(ByVal hWnd As LongPtr, _
ByVal hWndInsertAfter As LongPtr, _
ByVal X As LongPtr, _
ByVal Y As LongPtr, _
ByVal cx As LongPtr, _
ByVal cy As LongPtr, _
ByVal uFlags As LongPtr) As Long

Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

My frame A (named: MainWindow) has a button 'StartButton' which has below code when clicked. My frame B is named as 'smackingFrame'
Code:
Private Sub StartButton_Click()
MainWindow.Hide
smackingFrame.Show

Const C_VBA6_USERFORM_CLASSNAME = "smackingFrame"

Dim ret As Long
Dim formHWnd As Long

'Get window handle of the userform
formHWnd = FindWindow(C_VBA6_USERFORM_CLASSNAME, smackingFrame.Caption)
If formHWnd = 0 Then
Debug.Print Err.LastDllError
End If

'Set userform window to 'always on top'
ret = SetWindowPos(formHWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
If ret = 0 Then
Debug.Print Err.LastDllError
End If

Application.WindowState = xlMinimized

End sub
 
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
1. Please don't bump your posts so often. Wait at least a day.
2. The class name in the code is wrong - you should not change that part.
3. Unless your second form is modeless this approach won't work because this code stops as soon the second form is shown. The code needs to be in the activate event of the second form.

I can't make the changes for you as I'm using a tablet.
 
Upvote 0
Thanks for your reply Rory! I was just in a hurry as it was quiet urgent!

1. I've changed the ClassName to 'ThunderDFrame'
2. Also I've made Frame B - a Modeless

But Frame B just stands on top of Excel applications alone, but not on top of other windows. I know that we should use Application.WindowState = xlMinimized, but not sure if I've placed it at the right position.

What should I do to make my frame stand on top of all the windows, not just Excel app's????

Now my StartButton looks as below
Code:
Private Sub StartButton_Click()
 MainWindow.Hide
 smackingFrame.Show (0)
 
 Const C_VBA6_USERFORM_CLASSNAME = "ThunderDFrame"
    
 Dim ret As Long
 Dim formHWnd As Long
   
 'Get window handle of the userform
  formHWnd = FindWindow(C_VBA6_USERFORM_CLASSNAME, Me.Caption)
  If formHWnd = 0 Then
    Debug.Print Err.LastDllError
  End If


 'Set userform window to 'always on top'
  ret = SetWindowPos(formHWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
  If ret = 0 Then
    Debug.Print Err.LastDllError
  End If

Application.WindowState = xlMinimized


End Sub
 
Last edited by a moderator:
Upvote 0
Just an opinion, but save some quick message box, wouldn't making something essentially system modal seem risky?
 
Upvote 0
Do you have two user accounts? We do not allow that.
 
Upvote 0
@ TangoCharlie:

Any lights on this? Even I wanted to make my frame float on top of other windows, not just on Excel.

Thus far, correct or mistaking, I take the OP's request:

...What should I do to make my frame stand on top of all the windows(bolding added - mws), not just Excel app's????...

...to mean system modal. That is to say, I think that Vijaykumarnachapalli's request is to not allow any other visible window to receive the focus whilst the userform is up. My comment thus far was only to the effect that other than a simple choice and not much code behind the form, this seems risky. We will need to wait for Vijaykumarnachapalli to clarify I imagine.

Mark
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,698
Members
449,117
Latest member
Aaagu

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