Sub CreerEmail()
'
Dim OutlookApp As Object, OutlookMail As Object
Dim ws As Worksheet
Dim cell As Range
Dim emailAddress As String, bodyText As String
Dim currentRow As Long
' Définir la feuille
Set ws = ThisWorkbook.Sheets("Inscript Rando & Scan accueil")
' Vérifier si une cellule dans la colonne Y est sélectionnée
If Not Intersect(Selection, ws.Range("Y4:Y1100")) Is Nothing Then
Set cell = Selection
' Récupérer l'adresse email
emailAddress = ws.Cells(cell.Row, "U").Value
' Commencer à rédiger le corps de l'email
bodyText = "Veuillez trouver ci-joint la confirmation de votre inscription." & vbCrLf & vbCrLf
currentRow = cell.Row
' Collecter les données jusqu'à ce qu'il trouve "X" dans la colonne F ou jusqu'à la fin de la plage de données
Do
bodyText = bodyText & "Nom: " & ws.Cells(currentRow, "G").Value & vbCrLf
bodyText = bodyText & "Prénom: " & ws.Cells(currentRow, "H").Value & vbCrLf
bodyText = bodyText & "Code: " & ws.Cells(currentRow, "V").Value & vbCrLf
bodyText = bodyText & "Choix: " & ws.Cells(currentRow, "F").Value & vbCrLf & vbCrLf
currentRow = currentRow + 1
Loop While currentRow <= 1100 And UCase(ws.Cells(currentRow, "F").Value) <> "X"
' Créer un email dans Outlook après avoir collecté toutes les données
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.To = emailAddress
.Subject = "Confirmation d'inscription"
.Body = bodyText
.Display ' Afficher l'email (utiliser .Send pour envoyer directement)
End With
' Nettoyer
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Else
MsgBox "Veuillez sélectionner une cellule dans la colonne Y4:Y1100.", vbExclamation
End If
End Sub