View Single Post
 
Old 12-21-2024, 07:38 PM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default Keep Word UserForm on top of other documents

I have a standard code module and userform in a word document.


When the userform is displayed, I have managed to keep that form displayed regardless if I switch to another open document, open a different document or launch or switch to another application.


My goals is to keep the userform displayed ONLY if I switch to another open document or open another document. If I switch to another application or launch another application, I do not want the userform to display.


Thanks for any help. The document is also attached.



Standard module code:


Code:
Option Explicit
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const HWND_TOP = 0
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2

#If VBA7 Then
  Public Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hWnd As LongPtr, ByVal hWndInsertAfter As LongPtr, _
       ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal uFlags As Long) As Long
  Public Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
       ByVal lpWindowName As String) As LongPtr
  Public Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, _
        ByVal nIndex As Long) As Long
  Public Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
        (ByVal hWnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  Public Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hWnd As LongPtr) As Long
  Public Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal hWnd As LongPtr) As Long
  
#Else
  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 uFlags As Long) As Long
  Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
       ByVal lpWindowName As String) As Long
  Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, _
        ByVal nIndex As Long) As Long
  Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
        (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  Public Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
#End If
Dim oFrm As frmDemo
Sub ShowFormOnTopofApp()
  Set oFrm = New frmDemo
  oFrm.Show vbModeless
lbl_Exit:
  Exit Sub
End Sub



Userform code:


Code:
Option Explicit
Private Sub UserForm_Initialize()
  AlwaysOnTop Me.caption
End Sub
Private Sub AlwaysOnTop(caption As String)
#If VBA7 Then
  Dim hWnd As LongPtr
#Else
  Dim hWnd As LongPtr
#End If
Dim lngResult As Long
  hWnd = FindWindow("ThunderDFrame", caption)
  If hWnd <> 0 Then
    'This keeps the form displayed if another document or different application is launched.
    lngResult = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    'Trying to adapt to stet out the above line and keep form displayed only if another document in Word is opened (or switched to another already open document).
    'It seems like the following should work but is doesn't.
    lngResult = SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    End If
lbl_Exit:
  Exit Sub
End Sub
Attached Files
File Type: docm OnTop - Keep Userform OnTop.docm (32.3 KB, 3 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote