1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
Public Function SendEMail()
On Error GoTo Err_SendEmail
Dim db As DAO.Database: Set db = CurrentDb
Dim rst As DAO.Recordset
Dim strSQL As String
Dim objOutlook As New Outlook.Application
Dim objMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strSQL = "SELECT qryEmailList.ProjectRef, qryEmailList.email,qryEmailList.PO,qryEmailList.TM_eMail,qryEmailList.RemindersendOn,QryEmailList.Reminder2On,QryEmailList.Reminder3On,QryEmailList.Reminder4On,QryEmailList.Reminder5On,QryEmailList.Reminder6On, qryEmailList.SendingDate FROM qryEmailList;"
Set rst = db.OpenRecordset(strSQL)
While rst.EOF = False
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(olMailItem)
'With OutMail
With objMail
.To = rst("email")
.CC = rst("TM_eMail")
.Body = "Dear ... Blablabla "
.BCC = "DeliverablesReminders@xxxxxx.xx"
.Subject = rst("ProjectRef")
'On change l'expéditeur e-mail en faisant appel à la fonction chooseemail
.SendUsingAccount = OutApp.Session.Accounts.Item(2) 'Ici ce serait l'adresse # 2 du code d'avant
.Send
' .Display: 'display le message en cas de nécessité pour tester le code .. naturellement mettre en commentaire le .sent
End With
rst.Edit
rst("Sendingdate") = DateAdd("d", 30, Date)
rst.Update
Set objMail = Nothing
Set objOutlook = Nothing
rst.MoveNext
rst.Update
Wend
Set rst = Nothing
Set db = Nothing
While rst.EOF = False
If IsNull(rst("sendingdate")) Then
rst("remindersendon") = Date
If IsNull(rst("reminder2On")) Then
rst("reminder2On") = Date
If IsNull(rst("reminder3On")) Then
rst("reminder3on") = Date
If IsNull(rst("reminder4On")) Then
rst("reminder4on") = Date
If IsNull(rst("reminder5On")) Then
rst("reminder5on") = Date
If IsNull(rst("reminder6On")) Then
rst("reminder6On") = Date
End If
rst.MoveNext
Wend
rst.Close
Set rst = Nothing
Set db = Nothing
Set objMail = Nothing
Set objOutlook = Nothing
Exit_SendEmail:
DoCmd.SetWarnings True
DoCmd.Hourglass False
Exit Function
Err_SendEmail:
sErr = "Error " & Error & " / " & Err
MsgBox sErr, vbInformation + vbOKOnly, "Error on Email function"
Resume Exit_SendEmail
End Function |
Partager