Option Explicit
Sub Invia_Click()
Dim mail As String
Dim ccmail As String
Dim allegato As String
Dim j As Integer
Dim conta As Integer
Dim rng As Range
Dim fso As Object, f As Object
Range("R2:R10000").ClearContents
Set fso = CreateObject("Scripting.FileSystemObject")
j = 2
While Trim(Cells(j, 2)) <> ""
conta = Application.WorksheetFunction.CountIf(Range("A:A"), Cells(j, 1))
Set rng = Range(Cells(j, 1), Cells((j + conta - 1), 8))
mail = Cells(j, 9)
ccmail = Cells(j, 10)
allegato = ""
For Each f In fso.GetFolder(Cells(j, 12)).Files
If UBound(Split(f.Name, Cells(j, 1))) > 0 Then
'soltanto il primo file che contiene il codice cliente viene tenuto buono
allegato = f.Name
Exit For
End If
Next
If allegato <> "" Then
Call Inviamail(mail, ccmail, allegato, j, conta, Union(Range("A1:H1"), rng))
End If
j = j + conta
Wend
Set fso = Nothing
Set f = Nothing
End Sub
|