Bonjour à tous,
J'ai un fichier excel (test) avec une liste de nom (B) et leur adresse mail (C).
Le but est que si un fichier .pdf au nom (B) existe dans le fichier C:\Pers2 alors il envoie le mail sinon il continue son chemein.
Je bloque sur la sortie de la boucle for ?
Quelqu'un pourrait-il m'aider à sortir de la For si "sAttachmentFileName" n'est pas présent dans "sAttachmentPath"
Merci à tous
Habiler
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Sub SendEmailsWithAttachments() Dim objOutlook As Object Dim objMail As Object Dim lRow As Long Dim sTo As String Dim fItem As String Dim sSubject As String Dim sBody As String Dim sAttachmentPath As String ' Set the path to the directory containing the files to be attached sAttachmentPath = "C:\pers2\" ' Create an instance of Outlook Set objOutlook = CreateObject("Outlook.Application") ' Loop through all rows in the Excel worksheet For lRow = 2 To Cells(Rows.Count, "A").End(xlUp).Row sTo = Cells(lRow, 3).Value sSubject = Cells(lRow, 2).Value sBody = Cells(lRow, 3).Value ' Get the name of the attachment file Dim sAttachmentFolder As String Dim sAttachmentFileName As String sAttachmentFileName = Cells(lRow, 2).Value & ".pdf" sAttachmentFolder = sAttachmentPath & sAttachmentFileName ' Create a new email Set objMail = objOutlook.CreateItem(0) With objMail .To = sTo .Subject = sSubject .Body = sBody ' Add the attachment file Debug.Print sAttachmentFolder .Attachments.Add sAttachmentFolder .Display 'sAttachmentFolder End With ' Clean up Set objMail = Nothing Next lRow ' Clean up Set objOutlook = Nothing End Sub
Partager