Sub Envoi_Mail()

    ' On définit tout nos objets
    
    Dim OutApp As Object, OutMail As Object, Signature As String
    Dim cell As Range, rng As Range, rngSelection As Range
    Dim selectedSheet As Worksheet, mailSheet As Worksheet
    Dim eMail As String
    Dim prenom As String
    Dim nom As String
    Dim ETAT As String
    Dim corpsTexte As String, formuleBonjour As String
    Dim msg As String, response As Integer
    Dim EtatPareil As Boolean
    Dim sujet As String

    Set OutApp = CreateObject("Outlook.Application")
    Set selectedSheet = ActiveSheet ' Feuille active
    Set mailSheet = Worksheets("MAIL")
    Set rngSelection = Selection
    
    Dim wsCandidats As Worksheet
    Set wsCandidats = Worksheets("Candidats")
    
    Dim colPrenomCand As Integer
    Dim colNomCand As Integer
    Dim colMailCand As Integer
    Dim colEtatCand As Integer
    
    'Nous servira pour vérifier les doublons
    Dim emailCollection As Collection
    Set emailCollection = New Collection
    
    ' On utilise Cherche_ColonneXL pour trouver les colonnes
    colPrenomCand = Cherche_ColonneXL(wsCandidats, "PRENOM")
    colNomCand = Cherche_ColonneXL(wsCandidats, "NOM")
    colMailCand = Cherche_ColonneXL(wsCandidats, "MAIL")
    colEtatCand = Cherche_ColonneXL(wsCandidats, "ETAT")
    
    ' On empêche la sélection de plusieurs lignes
    
    If Selection.Columns.Count > 1 Then
        MsgBox "Merci de ne sélectionner qu'une seule colonne. Vous pouvez uniquement sélectionner plusieurs lignes.", vbExclamation, "Erreur"
        Exit Sub
    Else

    ' On récupère la signature enregistrée dans Outlook
    Set OutMail = OutApp.CreateItem(0)
    Signature = OutMail.HTMLBody
    OutMail.Close False ' Ferme le mail utilisé pour récupérer la signature sans l'envoyer

    ' On adapte la récupération des informations en fonction de la feuille active
    
    If selectedSheet.Name = "CANDIDATS" Then
        If rngSelection.Rows.Count = 1 Then ' Un seul candidat sélectionné
            eMail = selectedSheet.Cells(rngSelection.Row, colMailCand).Value
                     
            If eMail = "" Then
              MsgBox "Pas d'e-mail détecté", vbExclamation, "Erreur"
              Exit Sub
            End If
                        
            prenom = selectedSheet.Cells(rngSelection.Row, colPrenomCand).Value
            nom = selectedSheet.Cells(rngSelection.Row, colNomCand).Value
            ETAT = selectedSheet.Cells(rngSelection.Row, colEtatCand).Value
        Else
            ' Plusieurs candidats sélectionnés
            Set rng = rngSelection
            ETAT = selectedSheet.Cells(rngSelection.Row, colEtatCand).Value
            EtatPareil = True
            sujet = mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "C").Value
            
            For Each cell In rng
                If selectedSheet.Cells(cell.Row, colEtatCand).Value <> ETAT Then
                    EtatPareil = False
                    Exit For
                End If
            Next cell
            If EtatPareil Then
                
            sujet = mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "C").Value

                
                On Error Resume Next 'ignore les erreurs
                
                For Each cell In rng
                    emailCollection.Add selectedSheet.Cells(cell.Row, colMailCand).Value, CStr(selectedSheet.Cells(cell.Row, colMailCand).Value)
                Next cell
                On Error GoTo 0
                
                eMail = ""
                
                For i = 1 To emailCollection.Count
                    If i <> emailCollection.Count Then
                        eMail = eMail & emailCollection.Item(i) & ";"
                    Else
                        eMail = eMail & emailCollection.Item(i)
                    End If
                Next i
                
                formuleBonjour = "Bonjour,<br><br>" & mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "B").Value
            
            Else
            
                ' Si les candidats ont des états différents
                msg = "Tous les candidats n'ont pas le même état, voulez-vous quand même ouvrir Outlook?"
                response = MsgBox(msg, vbYesNo + vbQuestion, "Etats différents")
                If response = vbYes Then
                    eMail = selectedSheet.Cells(rng.Rows(1).Row, colMailCand).Value
                    For Each cell In rng
                        eMail = eMail & ";" & selectedSheet.Cells(cell.Row, colMail).Value
                    Next cell
                    corpsTexte = ""
                    formuleBonjour = "Bonjour,<br><br>"
                    sujet = ""
                Else
                    Exit Sub
                End If
            End If
        End If
        
    ElseIf selectedSheet.Name = "RESULTATS" Or selectedSheet.Name = "FOCUS_VIVIER" Or selectedSheet.Name = "FOCUS_ENTRETIEN" Then

    Set wsCandidats = ActiveSheet

    Dim colPrenomAutre As Integer
    Dim colNomAutre As Integer
    Dim colMailAutre As Integer
    Dim colEtatAutre As Integer
    
    ' On utilise Cherche_ColonneXL pour trouver les colonnes
    colPrenomAutre = Cherche_ColonneXL(wsCandidats, "PRENOM")
    colNomAutre = Cherche_ColonneXL(wsCandidats, "NOM")
    colMailAutre = Cherche_ColonneXL(wsCandidats, "MAIL")
    colEtatAutre = Cherche_ColonneXL(wsCandidats, "ETAT")
        
        If rngSelection.Rows.Count = 1 Then ' Un seul candidat sélectionné
        
        If colPrenomAutre = 0 Or colNomAutre = 0 Or colMailAutre = 0 Or colEtatAutre = 0 Then
            MsgBox "Une ou plusieurs colonnes nécessaires n'ont pas été trouvées.", vbExclamation, "Erreur"
            Exit Sub
        End If
        
        eMail = selectedSheet.Cells(rngSelection.Row, colMailAutre).Value
        
            If eMail = "" Then
              MsgBox "Pas d'e-mail détecté", vbExclamation, "Erreur"
              Exit Sub
            End If
            
            prenom = selectedSheet.Cells(rngSelection.Row, colPrenomAutre).Value
            nom = selectedSheet.Cells(rngSelection.Row, colNomAutre).Value
            ETAT = selectedSheet.Cells(rngSelection.Row, colEtatAutre).Value
        Else
            ' Plusieurs candidats sélectionnés
            Set rng = rngSelection
            ETAT = selectedSheet.Cells(rng.Rows(1).Row, colEtatAutre).Value
            EtatPareil = True
          
            If EtatPareil Then
        
            On Error Resume Next
            
            For Each cell In rng
                emailCollection.Add selectedSheet.Cells(cell.Row, colMailAutre).Value, CStr(selectedSheet.Cells(cell.Row, colMailAutre).Value)
            Next cell
            On Error GoTo 0
            
            eMail = ""
            For i = 1 To emailCollection.Count
                If i <> emailCollection.Count Then
                    eMail = eMail & emailCollection.Item(i) & ";"
                Else
                    eMail = eMail & emailCollection.Item(i)
                End If
            Next i
                formuleBonjour = "Bonjour,<br><br>" & mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "B").Value
                sujet = mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "C").Value
            Else
                ' Si les candidats ont des états différents
                msg = "Tous les candidats n'ont pas le même état, voulez-vous quand même ouvrir Outlook?"
                response = MsgBox(msg, vbYesNo + vbQuestion, "Etats différents")
                If response = vbYes Then
                    eMail = selectedSheet.Cells(rng.Rows(1).Row, colMailAutre).Value
                    For Each cell In rng
                        eMail = eMail & ";" & selectedSheet.Cells(cell.Row, colMailAutre).Value
                    Next cell
                    corpsTexte = ""
                    formuleBonjour = "Bonjour,<br><br>"
                    sujet = ""
                Else
                    Exit Sub
                End If
            End If
        End If
    End If

    If eMail = "" Then
        MsgBox "Pas d'adresse mail détectée", vbExclamation, "Erreur"
        Exit Sub
    End If

    If rngSelection.Rows.Count = 1 Then
        corpsTexte = mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "B").Value
        sujet = mailSheet.Cells(Application.Match(ETAT, mailSheet.Range("A:A"), 0), "C").Value
        formuleBonjour = "Bonjour " & nom & " " & prenom & ",<br><br>"
    End If

    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = eMail
        .Subject = sujet
        .HTMLBody = formuleBonjour & corpsTexte & "<br>" & Signature ' Ajoute la signature Outlook par défaut mais visiblement
        .Display ' Affiche l'aperçu avant d'envoyer encore une fois
    End With
    Set OutMail = Nothing
    Set OutApp = Nothing

End If
    
End Sub
