Mail.bas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Option Compare Database
  2. Public Sub Mail_Maj()
  3. Dim VERSION, modif, lien As String
  4. Dim DateVersion As Date
  5. Dim sujet, str As String
  6. 'avertissement
  7. lien = parametre("Lien_MAJ")
  8. If Not Len(lien) > 0 Then Exit Sub
  9. If MsgBox("ATTENTION: Votre version de l'application n'est pas à jour. Voulez-vous qu'un mail de mise à jour vous soit envoyé?", vbYesNo) = vbNo Then Exit Sub
  10. sujet = "AUTOMATIQUE - Mise à jour " & CurrentDb.Properties("AppTitle")
  11. str = "<html>" & vbCrLf & _
  12. "<body>" & vbCrLf & _
  13. "Bonjour, <br><br>" & vbCrLf
  14. 'version:
  15. DateVersion = DMax("Version", "ztblVersion", "")
  16. If DateVersion > #1/1/1990# Then VERSION = " en version: <b>" & Nz(DLookup("Version_Lb", "ztblVersion", "[Version]=#" & Format(DateVersion, "mm/dd/yyyy") & "#"), "") & " (" & DateVersion & ")</b><br>"
  17. If Not Len(VERSION) > 0 Then VERSION = " dans une nouvelle version."
  18. str = str & " L'application <b>" & CurrentDb.Properties("AppTitle") & "</b> est disponible " & VERSION & vbCrLf
  19. 'modifs
  20. If DateVersion > #1/1/1990# Then modif = Nz(DLookup("Modifications", "ztblVersion", "[Version]=#" & Format(DateVersion, "mm/dd/yyyy") & "#"), "")
  21. If Len(modif) > 0 Then
  22. str = str & _
  23. " Les modifications suivantes ont été apportées: <br><br>" & vbCrLf & _
  24. " " & modif & "<br><br>" & vbCrLf
  25. End If
  26. 'fin du message
  27. lien = "<a href='" & lien & "'>ici</a><br>"
  28. str = str & _
  29. " Pour mettre à jour, cliquez " & lien & "<br>" & vbCrLf & _
  30. "Bonne journée!" & vbCrLf & _
  31. "</body>" & vbCrLf & _
  32. "</html>"
  33. Call EnvoiMail(sujet, str, , True)
  34. 'l'absence de destinataire défini enverra le mail à l'utilisateur (il se l'envoie à lui-même)
  35. MsgBox "Le mail a été envoyé, vous devriez le recevoir d'ici quelques instants."
  36. End Sub
  37. Public Sub EnvoiMail(ByVal sujet As String, ByVal texte As String, Optional dest As String, Optional EnvoiAuto As Boolean)
  38. 'si le destinataire n'est pas precisé, le mail est envoyé à soi-même (pour un mail de MAJ par exemple)
  39. Dim olApp As Outlook.Application
  40. Dim objMail As Outlook.MailItem
  41. Dim oAccount As Outlook.Account
  42. Set olApp = GetObject("", "Outlook.Application")
  43. Dim olExplorer As Outlook.Explorer
  44. Dim html As Integer
  45. html = 0
  46. If Left(texte, 6) = "<html>" Then html = 1
  47. ' Créons un objet Mail qui va nous servir de base pour définir les paramètres et le contenu de notre mail
  48. Set objMail = olApp.CreateItem(olMailItem)
  49. 'Maintenant nous allons créer un object nous permettant de nous déplacer dans les dossiers d’outlook.
  50. Dim mpf As Outlook.MAPIFolder
  51. 'Dans quel format voulons nous notre Mail Texte Brut ou Texte Enrichi
  52. If html = 1 Then
  53. objMail.BodyFormat = olFormatHTML
  54. Else
  55. objMail.BodyFormat = olFormatRichText
  56. End If
  57. 'Affiche le mail dans Outlook
  58. 'Sans cette ligne la fenêtre n’est pas visible
  59. If Nz(EnvoiAuto, False) = False Then objMail.Display
  60. 'Affectation du sujet du mail
  61. 'Idéalement, vous utiliserez dans votre propre code une variable ou un paramètre via l’appel de cette procédure
  62. objMail.Subject = sujet
  63. 'Affectation du corps du message, le Body…
  64. If html = 1 Then
  65. objMail.HTMLBody = texte
  66. Else
  67. objMail.Body = texte
  68. End If
  69. 'Affectation du destinataire du message
  70. If Not Len(dest) > 0 Then
  71. For Each oAccount In olApp.Session.Accounts
  72. dest = oAccount.SmtpAddress
  73. Next
  74. End If
  75. objMail.To = dest
  76. 'Affectation des destinataires en Copie ou en copie cachée
  77. 'objMail.Cc = "-@cg67.fr"
  78. 'objMail.BCC= "-@cg67.fr"
  79. ' Et voila, vous avez prérempli votre mail au sein d’outlook
  80. ' Il ne vous reste plus que de compléter éventuellement votre mail à la main et de cliquer sur Envoyer
  81. 'Si vous souhaitez forcer l’envoi directement depuis le code VBA, sans laisser le temps à l’utilisateur de relire le mail
  82. ' il vous suffit de faire appel à :
  83. If Nz(EnvoiAuto, False) = True Then objMail.Send
  84. Set olApp = Nothing
  85. Set objMail = Nothing
  86. End Sub