Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function envoiMail(txt As String)
- 'Envoi d'un mail fonction trouvée ici :
- 'https://forum.excel-pratique.com/viewtopic.php?t=38945
- Dim mMessage As Object
- Dim mConfig As Object
- Dim mChps
- Set mConfig = CreateObject("CDO.Configuration")
- mConfig.Load -1
- Set mChps = mConfig.Fields
- With mChps
- .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
- 'Adapter suivant votre serveur de mail. (exemple pour Gmail.)=> Hormail "smtp.live.com"
- .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.free.fr"
- 'En principe, 25 fonctionne avec tout les serveurs.
- .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
- 'Vous pouvez essayer sans ces trois lignes
- 'Mais si votre serveur demande une authentification,
- '.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
- '.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "MyMail@gmail.com"
- '.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "MyCodePass"
- 'Si votre serveur demande une connexion sûre (SSL)
- '.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = "true"
- .Update
- End With
- Set mMessage = CreateObject("CDO.Message")
- With mMessage
- Set .Configuration = mConfig
- .To = "toto@toto.com" 'Email du destinataire
- .From = "toto@toto.com" 'Email de l'expediteur
- .Subject = "Mise à jour des permis conducteurs - " & Date
- .TextBody = txt
- .Send
- End With
- 'Libère les ressources
- Set mMessage = Nothing
- Set mConfig = Nothing
- Set mChps = Nothing
- End Function
- ' Ce qui va se passer à chaque ouverture du fichier Excel
- Private Sub Workbook_Open()
- 'Récupération d'information feuille "Config"
- dernierEnvoi = Sheets("config").Range("A2").Value
- 'Si l'email a déjà été envoyé aujourd'hui, pas besoin de le refaire !
- If dernierEnvoi = Date Then
- MsgBox "Mail déjà envoyé aujourd'hui"
- Exit Sub
- End If
- 'On donne une valeur à la variable txtPermis
- txtPermis = "ATTENTION !" & Chr(13) & Chr(13)
- 'Initialisation de la boucle
- For i = 2 To 1048576
- ' Pour ne pas faire toutes les lignes d'excel, on s'arrète quand la ligne est vide
- If IsEmpty(Range("A" & i)) Then
- Exit For
- End If
- ' On ajoute les messages selon la date de validité du permis
- If Range("C" & i) - Date < 0 Then
- txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " est expiré" & Chr(13) & Chr(13)
- nAffichage = nAffichage + 1
- ElseIf Range("C" & i) - Date < 30 Then
- txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " expire dans " & Range("C" & i) - Date & " jours" & Chr(13) & Chr(13)
- nAffichage = nAffichage + 1
- ElseIf Range("C" & i) - Date < 183 And Sheets("config").Range("B" & i) <= Date Then
- txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " expire dans moins de 6 mois (Le " & Range("C" & i) & ")" & Chr(13) & Chr(13)
- Sheets("config").Range("B" & i).Value = Date + 7
- nAffichage = nAffichage + 1
- ElseIf Range("C" & i) - Date < 365 And Sheets("config").Range("B" & i) <= Date Then
- txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " expire dans moins d'un an" & Chr(13) & Chr(13)
- Sheets("config").Range("B" & i).Value = Date + 30
- nAffichage = nAffichage + 1
- End If
- 'Fin de la boucle
- Next
- ' Si il y a des messages, on envoi l'email, sinon, on ne fait rien !
- If nAffichage > 0 Then
- MsgBox txtPermis
- ' envoiMail (txtPermis)
- ' On modifie la date de dernier envoi et on sauvegarde le fichier
- Sheets("config").Range("A2").Value = Date
- ActiveWorkbook.Save
- End If
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement