Results 1 to 2 of 2
  1. #1
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Forms, minimizing / maximising (2003)

    Hello,

    If I have a document based on a template, which contains an add-in which opens a Userform for the user when a new document is created.. is there a way to minimise the Word Application window and have the Form displayed *on top* of other windows that the user may have open?

    At the moment I have:

    Sub AutoNew()
    Call AddMacros
    Application.WindowState = wdWindowStateMinimize
    Application.Run "Properties.DefineDocProperties"
    Application.WindowState = wdWindowStateNormal
    End Sub

    Which almost works fine, except the 'Doc Properties' Userform is then hidden behind any other application window that the user may have open. Is there any way to force it to the front?

    I don't absolutely have to hide the Word application window, but it looks a bit cleaner and is a bit faster (even if screen updating is switched off).

    cheers

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Forms, minimizing / maximising (2003)

    You can use this code behind your userform:
    <code>
    Const HWND_TOPMOST = -1
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const SWP_NOACTIVATE = &H10
    Const SWP_SHOWWINDOW = &H40

    Public hWnd As Long

    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)

    Private Sub UserForm_Activate()
    SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, _
    SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    End Sub

    Private Sub UserForm_Initialize()
    hWnd = FindWindow("ThunderDFrame", Me.Caption)
    End Sub
    </code>
    The part up to and including SetWindowPos must be at the top of the module, before all other Subs and Functions.

    Source: How to show a Userform on top of other applications.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •