1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| Option Explicit
'
' Code à ajouter en début du module du formulaire pour rendre celui-ci nonModal multi-fenêtre
'
' By Jan Karel Pieterse - info@jkp-ads.com - http://www.jkp-ads.com/
'
Private Const GWL_HWNDPARENT As Long = -8
'Variable objet pour déclencher des événements d'application
Private WithEvents XLApp As Excel.Application
#If VBA7 Then
'Pour VBA 7 : Excel 2010 et après
Dim mXLHwnd As LongPtr 'handle de la fenêtre Excel
Dim mhwndForm As LongPtr 'handle de la fenêtre de cet userform
Private Declare PtrSafe Function FindWindowA Lib "user32" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#If Win64 Then
Private Declare PtrSafe Function SetWindowLongA Lib "user32" Alias "SetWindowLongPtrA" _
(ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function SetWindowLongA Lib "user32" _
(ByVal hwnd As LongPtr, ByVal nIndex As Long, ByVal dwNewLong As LongPtr) As LongPtr
#End If
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
'Pour VBA 6 : jusqu'à Excel 2007
Dim mXLHwnd As Long 'handle de la fenêtre Excel
Dim mhwndForm As Long 'handle de la fenêtre de cet userform
Private Declare Function FindWindowA Lib "user32" _
(ByVal lpClassName As String, ByVal lpWindowName As String) 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
#End If
Private Sub XLApp_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window)
If Val(Application.Version) >= 15 And mhwndForm <> 0 Then 'Formulaire créé dans la fenêtre active Excel.
mXLHwnd = Application.hwnd 'Défini systématiquement car en SDI chaque wb a sa propre fenêtre.
SetWindowLongA mhwndForm, GWL_HWNDPARENT, mXLHwnd
SetForegroundWindow mhwndForm
End If
End Sub
Private Sub XLApp_WindowResize(ByVal Wb As Workbook, ByVal Wn As Window)
If Not Me.Visible Then Me.Show vbModeless
End Sub
Private Sub XLApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
SetWindowLongA mhwndForm, GWL_HWNDPARENT, 0&
End Sub
Private Sub UserForm_Initialize()
If Val(Application.Version) >= 15 Then 'Excel 2013 et après
Set XLApp = Application
mhwndForm = FindWindowA("ThunderDFrame", Caption)
End If
'
' Fin du code à ajouter en début du module du formulaire
'
'
' Ajouter ici votre initialisation
End Sub |
Partager