Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 12-29-2024, 03:47 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,617
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I managed to cobble something together using application events.

Code:
Option Explicit
Private WithEvents oApp As Word.Application
#If VBA7 Then
  Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
  Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As LongPtr) As LongPtr
  Private Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
  Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
  Dim m_AppHwnd As LongPtr
  Dim m_UFHwnd As LongPtr
#Else
  Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As LongPtr) As Long
  Private Declare Function SetWindowLongA Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  Dim m_AppHwnd As Long
  Dim m_UFHwnd As Long
#End If
Const GWL_HWNDPARENT As Long = -8
Private Sub UserForm_Initialize()
Dim oDoc As Document
  If val(Application.Version) >= 15 Then
    m_AppHwnd = Application.ActiveWindow.hwnd
  Else
    m_AppHwnd = FindWindow("OpusApp", vbNullString)
  End If
  Set oApp = Application
lbl_Exit:
  Exit Sub
End Sub
Private Sub UserForm_Activate()
 IUnknown_GetWindow Me, VarPtr(m_UFHwnd)
  If m_UFHwnd = 0 Then m_UFHwnd = FindWindow("ThunderDFrame", Caption)
End Sub
Private Sub oApp_WindowActivate(ByVal Wb As Document, ByVal Wn As Window)
  If m_UFHwnd <> 0 Then
    If val(Application.Version) >= 15 Then
      m_AppHwnd = Application.ActiveWindow.hwnd
    Else
      m_AppHwnd = FindWindow("OpusApp", vbNullString)
    End If
    SetWindowLongA m_UFHwnd, GWL_HWNDPARENT, m_AppHwnd
    SetForegroundWindow m_UFHwnd
  End If
lbl_Exit:
  Exit Sub
End Sub
Private Sub oApp_WindowResize(ByVal Wb As Document, ByVal Wn As Window)
  If Not Visible Then Show vbModeless
lbl_Exit:
  Exit Sub
End Sub
Private Sub oApp_DocumentBeforeClose(ByVal Wb As Document, Cancel As Boolean)
  SetWindowLongA m_UFHwnd, GWL_HWNDPARENT, 0&
lbl_Exit:
  Exit Sub
End Sub
__________________
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 01:37 PM.


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