Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow

    Hi

    I need to add to a module some VBA to ensure that any template run in Word appears on the monitor on top of any other program running. So far I have tried:

    Sub New()
    Load frmX
    frmX.Show
    End Sub

    Sub New()
    Load frmX
    frmX.Show 1
    End Sub

    Sub New()
    Load frmX
    frmX.TopMost = True
    End Sub

    Sub New()
    Load frmX
    frmX.Show
    Application.WindowState = wdMaximized
    End Sub

    None of the above has had the desired effect. Has anyone any other ideas?

    Thanks

    Jennifgaye

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='jennifgaye' post='791647' date='01-Sep-2009 04:33']I need to add to a module some VBA to ensure that any template run in Word appears on the monitor on top of any other program running.[/quote]
    I think your code targets are particular UserForm, but the UserForm is a child of the Word application, so when the user switches away from Word, the UserForm will no longer be on top. Perhaps you need to make Word modal? That probably is not an option in the Word Object Model available in VBA, but you might be able to do it using the Windows API.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Welcome to the Lounge!

    I don't know if you still need this, but you can create a "topmost" userform with some Windows API code.

    At the top of the userform module:

    Code:
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_NOMOVE = &H2
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    
    Private Declare Function FindWindow Lib "user32" _
      Alias "FindWindowA" (ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long
     
    Private Declare Function SetWindowPos _
      Lib "user32.dll" (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
    In the Initialize event procedure:

    Code:
    Private Sub UserForm_Initialize()
      Dim hWnd As Long
      hWnd = FindWindow("ThunderDFrame", Me.Caption)
      If hWnd Then
    	SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
      End If
    End Sub
    Warning: users may not like this!

Posting Permissions

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