Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a form "Always On Top" Only to Excel? (NOT MODAL!) ?

Status
Not open for further replies.

NipsMG

Programmer
Dec 27, 2000
215
US
I've got a small problem.

I've got a userform displaying values to the user. I want this form to be "always on top" to the worksheet, but I also DON'T want it modal (I want the user to be able to interact with the spreadsheet, without the form losing focus and disappearing).

ANyone have any ideas?

Thanks! :)

--NipsMG
 
For Forms, just insert this into a module:
Code:
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOMOVE = &H2
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public 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

Public Sub ForceWindowOnTop(hwnd As Long, bTrueFalse As Boolean)
    Dim i
    If bTrueFalse = True Then
        i = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
    Else
        i = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
    End If
End Sub
Then, to call it, just pass the handle to the form and the desired setting.
Code:
'To Force Form on Top
Call ForceWindowOnTop(frmMain.hwnd, True)

'To Remove Forced Setting
Call ForceWindowOnTop(frmMain.hwnd, False)

'To Call from the form code, just pass Me.hwnd
Hope this helps...
 
Thanks for the reply... however, I know that method..

I don't want the form to be "Always on top" for EVERYTHING, JUST withing the Excel window..

This will make it so if I switch to another application, the form will be on top of that also.

I JUST want to make the for ALWAYS ON TOP of the spreadsheet it's attached to.

Any other ideas?

--NipsMG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top