Option
Explicit
Private
Declare
PtrSafe
Function
FindWindowEx
Lib
"user32"
_
Alias
"FindWindowExA"
( _
ByVal
hwndParent
As
LongPtr, _
ByVal
hwndChildAfter
As
LongPtr, _
ByVal
lpszClass
As
String
, _
ByVal
lpszWindow
As
String
)
As
LongPtr
Private
Declare
PtrSafe
Function
SetForegroundWindow
Lib
"user32"
( _
ByVal
hWnd
As
LongPtr)
As
Long
Private
Sub
Workbook_Activate()
Dim
objUsf
As
UserForm
Dim
hWnd
As
LongPtr
Debug.Print
Debug.Print Time$;
" # Workbook_Activate"
Debug.Print Time$;
" - looking for loaded user form"
For
Each
objUsf
In
VBA.UserForms
If
TypeName(objUsf) =
"UserForm1"
Then
Debug.Print Time$;
" - found user form '"
; TypeName(objUsf);
"'"
Exit
For
End
If
Next
If
objUsf
Is
Nothing
Then
Debug.Print Time$;
" - no relevant user form is currently loaded"
Else
Debug.Print Time$;
" - getting window handle of user form '"
; TypeName(objUsf);
"'"
hWnd = FindWindowEx(0, 0,
"ThunderDFrame"
, objUsf.Caption)
If
hWnd <> 0
Then
Debug.Print Time$;
" - window found (handle: "
& hWnd &
")"
Debug.Print Time$;
" - setting user form as foreground window"
Call
SetForegroundWindow(hWnd)
Else
Debug.Print Time$;
" - window was not found"
End
If
End
If
End
Sub