Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 12-21-2024, 07:38 PM
gmaxey gmaxey is offline Keep Word UserForm on top of other documents Windows 10 Keep Word UserForm on top of other documents Office 2019
Expert
Keep Word UserForm on top of other documents
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Userform once and apply the same selection to multiple documents Formd Word VBA 2 12-12-2017 10:11 AM
referenceing/finding word from one documents into another documents and indicating their location rok123 Word VBA 1 02-07-2016 04:50 PM
Userform calls other userform, then populate worksheet Lehoi Excel Programming 0 02-03-2016 02:58 PM
VBA Code in a UserForm module to delete a Command Button which opens the userform Simoninparis Word VBA 2 09-21-2014 03:50 AM
Microsoft Word 2013 Documents open as full version documents on two computers sporadically Jaydenc-Fortress Word 2 08-07-2014 12:34 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft