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 100
| Public Sub EnvoiMail2(unDestinataire As String, _
unSujet As String, _
unBody As String, unRepertoireAttach As String, _
unMaskAttach As String, peutModifier As Boolean)
On Error GoTo ErrorEnvoiMail2
' Déclare la variable contenant la référence à l'application OLE (Outlook ici).
Dim olApp As Object
' Déclare un item (un mail en l'occurence, mais peut être un contact, ...)
Dim mailItem As Object
' Déclare une liste de attachments
Dim listeAttachments As Object
' Déclare un attachment
Dim unAttachment As Object
' Déclare les trucs pour les fichiers
Dim unFichier ' Un fichier
Dim numFichier ' Numéro du fichier dans le dossier
' Crée l'objet Outlook.application
Set olApp = CreateObject("Outlook.Application")
' Crée un objet mail
Set mailItem = olApp.CreateItem(olMailItem)
' Attribue le sujet du mail
mailItem.Subject = unSujet
'attribue format texte brut au mail
' Attribue le corps du mail
'mailItem.HTMLBody = unBody
mailItem.Body = unBody
' Crée un destinataires
mailItem.To = unDestinataire
' Crée une liste d'attachments
Set listeAttachments = mailItem.Attachments
' Ajoute en attachment tous les fichiers portant le nom unMaskAttach (fichier, *.jp*,...)
' se trouvant dans tbx_attach_repertoire
' Extrait la première entrée.
unFichier = Dir(unRepertoireAttach & unMaskAttach, vbDirectory)
numFichier = 0
' Boucle tant qu'il y a des fichiers
Do While unFichier <> ""
' Ignore le dossier courant et le dossier contenant le dossier courant (. et ..)
If unFichier <> "." And unFichier <> ".." Then
' Ne récupère que les fichiers (des fois qu'un répertoire aurait le meme nom
If (GetAttr(unRepertoireAttach & unFichier) And vbDirectory) <> vbDirectory Then
numFichier = numFichier + 1
Set unAttachment = listeAttachments.Add(unRepertoireAttach & unFichier, 1) ', 3 , "Photo " + Str(numFichier))
End If
End If
' Extrait l'entrée suivante
unFichier = Dir
Loop
' Si on passe True au paramètre peutModifier, le mail s'affiche et on a
' la possibilité de le modifier
If peutModifier = True Then
' Affiche le mail
mailItem.Display
If MsgBox("Le mail généré est à présent terminé." + vbCrLf + _
"Vous pouvez dès à présent le modifier avant de l'envoyer, l'envoyer tout de suite" + vbCrLf + _
"en cliquant sur OK ou le supprimer en cliquant sur Annuler", _
vbOKCancel + vbQuestion + vbDefaultButton1, "Le mail est prêt") = vbOK Then
'message en premier plan + vbSystemModal apres Button1 mais alors plus de Display du mail'
' Envoie le mail
mailItem.Send
Else
' Supprime le mail
mailItem.Delete
End If
Else
' Envoie le mail
mailItem.Send
End If
' Fin de traitement
Exit_EnvoiMail2:
' Une fois terminé, utilise la méthode Quit pour fermer
'olApp.Quit
' puis libère la référence.
Set olApp = Nothing
' Quitte la procedure
Exit Sub
' Si une erreur se produit, on atterit ici
ErrorEnvoiMail2:
' Affiche le message d'erreur
MsgBox Err.Description
' Continue au label Exit_EnvoiMail
Resume Exit_EnvoiMail2
End Sub |
Partager