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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Word – make form stay on top of everything

Status
Not open for further replies.

msv6

Programmer
Sep 25, 2006
8
US
Does anyone know of a way to make a form stay on top of all applications or even on top of all open Word documents? I want a form that stays on top even if I minimize its parent Window (the document from which it was launched). I want it to stay on top even if I click on another Word document. Is it possible?

I've used the SetWindowPos function, but as soon as I minimize the form's parent, the form minimizes as well. I've tried using the SetParent function and the GetDesktopWindow function to make the desktop the forms parent, but for some reason the GetDesktopWindow seemed to return the same handle as the form's parent Word document.

Any help would be greatly appreciated.
 
This seems to work for me
Code:
Private Sub Form_Resize()
Dim WindowHandle As Long
WindowHandle = FindWindow(vbNullString, "NameOfOnTopWindow")
If Me.WindowState = vbMinimized And WindowHandle <> 0 Then
   Call SetWindowPos(WindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)
End If
End Sub
 
Thanks for the quick reply! Sorry for the delayed response.

First of all I'm quite new to VBA, so sorry if I misspeak. I'm using VBA 6.3 with Word 2000. I'm using a "UserForm", which does not seem to have a WindowState. I did, however, try (a slightly modified version of) your code snippet and it only made the Form stay on top until I clicked on another open Word document. What I really want is to make the form stay on top no matter what other document is active. I guess in a sense I'm wanting to separate the Form from its parent document.

I have a question for you though. An interesting thing happened when I used your code and I don't understand why. First, if you're interested, this next paragraph explains my application a bit:

The user opens a Word document, presses a toolbar button that loads a Form. While interacting with the Form, other Word documents are opened (often blocking the view of the form). The real pain is trying to view the opened documents and the Form together: If you click on the opened documents, the Form goes behind them, and if you click on the Form, the Form's parent document pops up and covers the opened documents.

Adding your code allowed me to close the form's parent document while the form remained opened; and I could switch between the Form and other open documents! Without your code, closing the parent always closed the form.

That's pretty much the best solution I've found so far to what I want. Thanks a ton!! (but I would like to understand why it works).

-msv6
 
OK - I found the EXACT functionality I want to try to replicate. And I still haven't found a solution that can replicate it. It must be possible because Word does it (somehow). I want to replicate the functionality of the Cross-reference dialog:

In Word (I know this works in 2000 and 20003):

- From the Insert menu, select Reference and then Cross-reference
- Open another Word document
- Click on the older document, then the newer, then the dialog

Notice how the dialog stays on top of all Word documents, and doesn't bring its original parent to the front when you drag it around, and doesn't disappear when you minimize one of the documents. You can even kill its original parent and it won’t go away.

I figure there's got to be a way to do this since it's built into Word!!

Any ideas? Thanks!
 
Thanks for your post Andy. Please correct me if I'm doing it wrong, but when I make the UserForm modal, the behavior I get is:

1. I can't select other documents behind the form (bring them to the fron)

2 I can bring another document to the front by clicking on it from the taskbar at the bottom of the screen, but it will cover the form, and as soon as I click on the form again, it's parent document pops up over the other document.

This is a bit different than the behavior of the Cross-reference dialog.

Thanks again.
-msv6
 
Sure, my mistake: one should always READ the post. :blush:

I think I found what you need, and it's making use of the API functions Golom already posted. You need an "Always on top" function, and I think I've found one:

Code:
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub 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)

Public Sub OnTop()

    Dim HW As Long
    
    HW = FindWindow(vbNullString, Word.ActiveDocument.Name & " - " & Word.Application.Caption)
  
    If HW <> 0 Then
        SetWindowPos HW, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    End If
  
End Sub


Public Sub NotOnTop()

    Dim HW As Long
    
    Debug.Print Word.ActiveDocument.Name & " - " & Word.Application.Caption
    
    HW = FindWindow(vbNullString, Word.ActiveDocument.Name & " - " & Word.Application.Caption)
  
    If HW <> 0 Then
        SetWindowPos HW, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
    End If
  
End Sub

I don't have the time to try it myself now, but according to the poster in that other forum, it's supposed to make the userform stay always on top.

I hope this does the trick.

Cheers,
Andy

[blue]Speak out against Human Rights violations in China/Tibet
[/blue]
 
Thanks again for the post Andy! If I call SetWindowPos (with HWND_TOPMOST) in the Form's activate subroutine, it the form is put on top, but it does not stay on top. Maybe I'm doing it wrong, but as far as I can tell, SetWindowPos does not permanently set a window's position. As soon as I click on some other open document, the form is no longer on top. If you notice on Word's Cross-reference dialog, for example, the form stays on top no matter what document becomes active, and you can still move/modify the active document. It's almost as if the dialog changes parents every time a different form is activated...

Thanks again,
msv6
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top