'*****************************************************************************************************
'    ___     _     _______  __      _   ____  _   _  _______  ___     _   _   _    ___     _     _.
'   //  \\  /\\      //    // \\   //  //    //  //    //    //  \\  //  //  //   //  \\  //|   //
'  //___// //__\    //    //__//  //  //    //__//    //    //   // //  //  //   //   // // |  //
' //      //   \\  //    //  \\  //  //    //  \\    //    //   // //  //  //   //   // //  | //
'//      //    // //    //   // //  //___ //    \\  //     \\__// //__//  //___ \\__// //   |//
'****************************************************************************************************
'Module classe permettant le Selreactivate de l'userform
'created by patricktoulon
#If VBA7 Then
    Private Declare PtrSafe Function GetActiveWindow Lib "user32.dll" () As LongPtr
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
#Else
    Private Declare Function GetActiveWindow Lib "user32.dll" () As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
#End If
Public WithEvents bt As MSForms.CommandButton
Public WithEvents lab As MSForms.Label
Public WithEvents lbx As MSForms.ListBox
Public WithEvents fram As MSForms.Frame
Public WithEvents txtb As MSForms.TextBox
Public WithEvents combo As MSForms.ComboBox
Public WithEvents img As MSForms.Image
Public WithEvents UforM As UserForm
Dim cls(1 To 100) As New FormSelfReactivate
#If VBA7 Then
    Public HandleForM As LongPtr
#Else
    Public HandleForM As Long
#End If
Public uf As Object
Public Function init(usf)
    Dim ctrl
    For Each ctrl In usf.Controls
        i = i + 1
        Select Case True
            Case TypeName(ctrl) = "CommandButton" Or TypeOf ctrl Is CommandButton: Set cls(i).bt = ctrl: Set cls(i).uf = usf
            Case TypeName(ctrl) = "ListBox" Or TypeOf ctrl Is ListBox: Set cls(i).lbx = ctrl: Set cls(i).uf = usf
            Case TypeName(ctrl) = "TextBox" Or TypeOf ctrl Is TextBox: Set cls(i).txtb = ctrl: Set cls(i).uf = usf
            Case TypeName(ctrl) = "Frame" Or TypeOf ctrl Is Frame: Set cls(i).fram = ctrl: Set cls(i).uf = usf
            Case TypeName(ctrl) = "Label" Or TypeOf ctrl Is Label: Set cls(i).lab = ctrl: Set cls(i).uf = usf
            Case TypeName(ctrl) = "Image" Or TypeOf ctrl Is Image: Set cls(i).img = ctrl: Set cls(i).uf = usf
            Case TypeName(ctrl) = "ComboBox" Or TypeOf ctrl Is ComboBox: Set cls(i).combo = ctrl: Set cls(i).uf = usf
        End Select
    Next
    Set UforM = usf
    Set uf = usf
End Function
Private Sub Class_Terminate()
    Erase cls
End Sub
Private Sub UForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub Combo_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub lbx_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub bt_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub Fram_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub Lab_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub TxtB_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Private Sub img_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single): bouge: End Sub
Public Sub bouge()
    Dim Control As MSForms.Control
    HandleForM = FindWindow(vbNullString, uf.Caption)
    '------------------------
    'UserForm n'est pas actif
    '------------------------
    If Not GetActiveWindow = HandleForM Then
        'Ré-activer le UserForm
        AppActivate uf.Caption
        Set Control = uf.ActiveControl
        'Force Control Activation
        With Control
            On Error Resume Next
            .Visible = Not .Visible
            .Visible = Not .Visible
            .SetFocus
        End With
    End If
End Sub