VBA w/ Word 2000 - keep form on top of all applications

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.



Answer this question

VBA w/ Word 2000 - keep form on top of all applications

  • hte

    OK - I found the EXACT functionality I want to try to replicate. 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!


  • Scottie S

    Hi msv6,

    Thought the old modal form would have done you.. Oh well I think your looking to keep the form on top. Most programming languages have a property of the form to keep it on top of all other applications, VBA doesn't. I believe you'll need to dip into the Windows API to get what your looking for.

    This link here might be what your looking for, I haven't ran it

    http://www.freevbcode.com/ShowMessage.asp ID=44299

    Also if you do a search for "Always on Top Window in VBA" or a variation of that you might find something better.

    Hope thats useful.



  • delas

    Thanks for your post Derek!

    Making the form modal doesn't do quite what I'm looking for though. If you try using Word's Cross-reference dialog, you'll notice it's not modal - it will stay on top of whatever Word document you activate, but it will let you move, view, and even modify other documents. When I make my UserForm modal - everything freezes until I close the form. Any Ideas Is this possible I thought it would be since Word does it somehow.

    Thanks again,

    msv6


  • borice

    That's called a modal form. Create a new Userform in your document, I'm presuming your using word here. It will be called UserForm1. To display the form use the following code.

    Dim usrFrm As New UserForm1
    usrFrm.Show vbModal

    It will open and stay open until the user deals with whatever is displayed.



  • DEEPAK GARG

    Hey Derek - Thanks for your reply. The link you sent me, and most things I find searching for "Always on TOp Window in VBA" all use the same approach: the Windows API call SetWindowPos( ). These solutions make the form stay on top of other forms; but - like Windows Cross-reference dialog - I'm looking for a way to make the UserForm stay on top of any running Word application. I've tried about every Windowing API I could find (including tyring to set the Form as a child to different things), but nothing worked.

    I don't have VB 6, I'm just developing with the VBA environment that comes with Word. Initially I was thinking there must be a way to do this, since the Cross-reference dialog does it, but since Word was written in C++ (I think), they could have used threads for the Cross-reference dialog. I don't think you can use threads with just the basic VBA that comes with Word. Maybe what I'm trying to do is not possible (without making a more complicated application with VB 6).

    Thanks again for your help!


  • VBA w/ Word 2000 - keep form on top of all applications