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 90 91 92 93 94 95 96 97 98 99
|
Option Explicit
Private Sub cmdEnvoyer_Click()
Dim strMsgErreur As String
Dim lngErreur As Long
Dim strDestinataires As String
Dim strObjet As String
Dim strTxtMessage As String
Dim strCC As String
Dim strBcc As String
Dim vntAttach As Variant
Dim blnEnvoyerSansVoir As Boolean
On Error GoTo L_ErrcmdEnvoyer_Click
strDestinataires = Nz(Me!txtDestinataires, "")
strObjet = Nz(Me!txtObjet, "")
strTxtMessage = Nz(Me!TxtMessage, "")
strCC = Nz(Me!txtCC, "")
strBcc = Nz(Me!txtBCC, "")
vntAttach = Split(Me.txtAttach, vbNullChar)
blnEnvoyerSansVoir = Nz(Me!chkEnvoyer, False)
If Len(strDestinataires) < 1Then
Me!txtDestinataires.SetFocus
Err.Raise 13, "Destinataire requis", "Euh, entre vous et moi, il est pour qui ce message ?"
End If
If Len(strObjet) < 1 Then
Me!txtObjet.SetFocus
Err.Raise 13, "Object requis", "Avec un objet du message, ça fait plus sérieux, non !"
End If
If Len(strTxtMessage) < 1 Then
Me!TxtMessage.SetFocus
Err.Raise 13, "Message requis", "Eh ben eh ben..., Vous voulez envoyer un message vide !"
End If
If EnvoyerMailOutlook(strMsgErreur, strDestinataires, strObjet, strTxtMessage, strCC, strBcc, vntAttach, blnEnvoyerSansVoir) = False Then
lngErreur = Split(strMsgErreur, " : ")(0)
Err.Raise lngErreur, "Envoi échoué", "Ooops la boulette : " & vbCrLf & strMsgErreur, vbExclamation
Else
MsgBox "C'est parti !", vbInformation
End If
On Error GoTo 0
L_ExcmdEnvoyer_Click:
Exit Sub
L_ErrcmdEnvoyer_Click:
MsgBox Err.Description, vbExclamation, Err.Source
Resume L_ExcmdEnvoyer_Click
End Sub
Public Function EnvoyerMailOutlook(MsgErreur As String, Destinataires As String, Objet As String, TxtMessage As String, CC As String, Bcc As String, Optional Attach As Variant, Optional ByVal Envoyer As Boolean = False) As Boolean
Dim appOutLook As Outlook.Application
Dim oEmail As Outlook.MailItem
Dim I As Integer
On Error GoTo L_ErrEnvoyerMailOutlook
Set appOutLook = New Outlook.Application
Set oEmail = appOutLook.CreateItem(olMailItem)
With oEmail
.To = Destinataires
.Subject = Objet
.HTMLBody = TxtMessage
.CC = CC
.Bcc = Bcc
If Not IsMissing(Attach) Then
If TypeName(Attach) = "String" Then
.Attachments.Add Attach
Else
For I = 0 To UBound(Attach) - 1
.Attachments.Add Attach(I)
Next
End If
End If
If Envoyer Then
.Send
Else
.Display
End If
End With
EnvoyerMailOutlook = True
On Error GoTo 0
L_ExEnvoyerMailOutlook:
Set appOutLook = Nothing
Set oEmail = Nothing
Exit Function
L_ErrEnvoyerMailOutlook:
EnvoyerMailOutlook = False
MsgErreur = Err.Number & " - " & Err.Description
Resume L_ExEnvoyerMailOutlook
End Function |
Partager