Advertisement
hztm

VBA envoi d'un email selon date validité

Mar 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function envoiMail(txt As String)
  2.     'Envoi d'un mail fonction trouvée ici :
  3.    'https://forum.excel-pratique.com/viewtopic.php?t=38945
  4.    Dim mMessage As Object
  5.     Dim mConfig As Object
  6.     Dim mChps
  7.    
  8.     Set mConfig = CreateObject("CDO.Configuration")
  9.    
  10.     mConfig.Load -1
  11.     Set mChps = mConfig.Fields
  12.     With mChps
  13.         .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  14.         'Adapter suivant votre serveur de mail. (exemple pour Gmail.)=> Hormail "smtp.live.com"
  15.        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.free.fr"
  16.         'En principe, 25 fonctionne avec tout les serveurs.
  17.        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  18.         'Vous pouvez essayer sans ces trois lignes
  19.        'Mais si votre serveur demande une authentification,
  20.        '.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
  21.        '.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "MyMail@gmail.com"
  22.        '.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "MyCodePass"
  23.        'Si votre serveur demande une connexion sûre (SSL)
  24.        '.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = "true"
  25.        .Update
  26.     End With
  27.    
  28.     Set mMessage = CreateObject("CDO.Message")
  29.     With mMessage
  30.     Set .Configuration = mConfig
  31.         .To = "toto@toto.com" 'Email du destinataire
  32.        .From = "toto@toto.com" 'Email de l'expediteur
  33.        .Subject = "Mise à jour des permis conducteurs - " & Date
  34.         .TextBody = txt
  35.         .Send
  36.     End With
  37.      'Libère les ressources
  38.    Set mMessage = Nothing
  39.     Set mConfig = Nothing
  40.     Set mChps = Nothing
  41. End Function
  42. ' Ce qui va se passer à chaque ouverture du fichier Excel
  43. Private Sub Workbook_Open()
  44.    
  45.     'Récupération d'information feuille "Config"
  46.    dernierEnvoi = Sheets("config").Range("A2").Value
  47.    
  48.     'Si l'email a déjà été envoyé aujourd'hui, pas besoin de le refaire !
  49.    If dernierEnvoi = Date Then
  50.         MsgBox "Mail déjà envoyé aujourd'hui"
  51.         Exit Sub
  52.     End If
  53.     'On donne une valeur à la variable txtPermis
  54.    txtPermis = "ATTENTION !" & Chr(13) & Chr(13)
  55.    
  56.     'Initialisation de la boucle
  57.    For i = 2 To 1048576
  58.         ' Pour ne pas faire toutes les lignes d'excel, on s'arrète quand la ligne est vide
  59.        If IsEmpty(Range("A" & i)) Then
  60.             Exit For
  61.         End If
  62.        
  63.         ' On ajoute les messages selon la date de validité du permis
  64.        If Range("C" & i) - Date < 0 Then
  65.             txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " est expiré" & Chr(13) & Chr(13)
  66.             nAffichage = nAffichage + 1
  67.         ElseIf Range("C" & i) - Date < 30 Then
  68.             txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " expire dans " & Range("C" & i) - Date & " jours" & Chr(13) & Chr(13)
  69.             nAffichage = nAffichage + 1
  70.         ElseIf Range("C" & i) - Date < 183 And Sheets("config").Range("B" & i) <= Date Then
  71.             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)
  72.             Sheets("config").Range("B" & i).Value = Date + 7
  73.             nAffichage = nAffichage + 1
  74.         ElseIf Range("C" & i) - Date < 365 And Sheets("config").Range("B" & i) <= Date Then
  75.             txtPermis = txtPermis & "Le permis de " & Range("B" & i) & " " & Range("A" & i) & " expire dans moins d'un an" & Chr(13) & Chr(13)
  76.             Sheets("config").Range("B" & i).Value = Date + 30
  77.             nAffichage = nAffichage + 1
  78.         End If
  79.    'Fin de la boucle
  80.   Next
  81.    
  82.    ' Si il y a des messages, on envoi l'email, sinon, on ne fait rien !
  83.   If nAffichage > 0 Then
  84.     MsgBox txtPermis
  85.    ' envoiMail (txtPermis)
  86.   ' On modifie la date de dernier envoi et on sauvegarde le fichier
  87.   Sheets("config").Range("A2").Value = Date
  88.    ActiveWorkbook.Save
  89.    End If
  90. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement