Option Explicit
#If VBA7 Then
#If Win64 Then
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hwnd As LongLong, ByVal nIndex As Long, ByVal dwNewLong As LongLong) As LongLong
#Else
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
#End If
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function ShowWindowAsync Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindowAsync Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
#End If
Private WithEvents AppEvents As Application
Private oForm As Object, oCombBox As Control
Private Sub Class_Initialize()
If Val(Application.Version) >= 15 Then
Set AppEvents = Application
End If
End Sub
Public Sub Init(ByVal Form As Object, Optional ByVal CombBox As Control)
If Not CombBox Is Nothing Then
Set oCombBox = CombBox
End If
Set oForm = Form
End Sub
Private Sub AppEvents_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window)
#If Win64 Then
Dim hwnd As LongLong
#Else
Dim hwnd As Long
#End If
Call IUnknown_GetWindow(oForm, VarPtr(hwnd))
If IsIconic(Wn.hwnd) Then
Wn.WindowState = xlNormal
End If
Call SetOwner(hwnd, Wn.hwnd)
Call SetForegroundWindow(hwnd)
Call ShowWindowAsync(hwnd, 1&)
End Sub
Private Sub AppEvents_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
#If Win64 Then
Dim hwnd As LongLong
#Else
Dim hwnd As Long
#End If
Call IUnknown_GetWindow(oForm, VarPtr(hwnd))
Call SetOwner(hwnd, 0)
End Sub
#If Win64 Then
Private Sub SetOwner(ByVal hwnd As LongLong, Owner As LongLong)
#Else
Private Sub SetOwner(ByVal hwnd As Long, Owner As Long)
#End If
Const GWL_HWNDPARENT = (-8)
Call SetWindowLong(hwnd, GWL_HWNDPARENT, Owner)
Call UpdateCombo
End Sub
Private Sub UpdateCombo()
Dim oWbk As Workbook
If Not oCombBox Is Nothing Then
With oCombBox
.Clear
For Each oWbk In Application.Workbooks
.AddItem oWbk.Name
Next
.Text = ActiveWorkbook.Name
End With
End If
End Sub