Option Explicit
Option Base 1
Dim AantalBestanden As Integer
Dim AantalBestandenGecheckt As Integer
Dim BestandsNaam As String
Dim AantalFout As Integer
Dim BestandFout() As Variant
Dim AantalGoed As Integer
Dim BestandGoed() As Variant
Dim BestandLocatie As String
Dim i As Integer
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant
Dim MailOnderwerp As String
Dim MailTekst As String
Private Sub Workbook_Open()
If Right(ActiveWorkbook.Name, 3) = "xlt" Or Right(ActiveWorkbook.Name, 3) = "XLT" Then
Else
'Bepaal aantal te controleren bestanden
Range("CheckAantal").Select
AantalBestanden = WorksheetFunction.Max(Range("A" & ActiveCell.Row & ":A" & (ActiveCell.Row + ActiveCell.CurrentRegion.Rows.Count)))
'declareer variabelen BestandFout() en BestandGoed() opnieuw
ReDim BestandFout(AantalBestanden) As Variant
ReDim BestandGoed(AantalBestanden) As Variant
'Leg locatie vast waar bestande staan die gecontroleerd moeten worden
BestandLocatie = Range("BestandLocatie").Value
'Stel variabelen AantalFout en AantalGoed op 0
AantalFout = 0
AantalGoed = 0
'Controleren bestanden
'Bepaal hoe vaak de lus doorlopen moet worden
For AantalBestandenGecheckt = 1 To AantalBestanden Step 1
'Bepaald naam van het te controleren bestand
BestandsNaam = ActiveCell.Offset(0, 1).Value
'Als DateModified van het bestand <> aan actuele datum, dan....
If FormatDateTime(FileDateTime(BestandLocatie & BestandsNaam), vbShortDate) <> _
FormatDateTime(Now(), vbShortDate) Then
'Hoog de teller voor het aantal foute bestanden op
AantalFout = AantalFout + 1
'Leg naam van een fout bestand vast in array-variabele BestandFout()
BestandFout(AantalFout) = Left(BestandsNaam, Len(BestandsNaam) - 4)
Else
'Hoog de teller voor het aantal goede bestanden op
AantalGoed = AantalGoed + 1
'Leg naam van een fout bestand vast in array-variabele BestandGoed()
BestandGoed(AantalGoed) = Left(BestandsNaam, Len(BestandsNaam) - 4)
End If
ActiveCell.Offset(1, 0).Select
Next
'Bepaal tekst in onderwerpregel en body van het mailtje
If AantalFout = 0 Then
MailOnderwerp = "Alle " & AantalBestanden & " bestanden zijn goed verwerkt door Optimiza"
MailTekst = "Goed verwerkte bestanden :<br>"
For i = 1 To AantalGoed
MailTekst = MailTekst & BestandGoed(i) & "<br>"
Next
Else
If AantalFout = AantalBestanden Then
MailOnderwerp = "Geen van de bestanden is goed verwerkt, zie mail voor toelichting"
MailTekst = "Niet verwerkte bestanden :<br>"
i = 1
For i = 1 To AantalFout
MailTekst = MailTekst & BestandFout(i) & "<br>"
Next
Else
MailOnderwerp = "Niet alle bestanden zijn juist verwerkt, zie mail voor toelichting"
MailTekst = "Goed verwerkte bestanden :<br>"
i = 1
For i = 1 To AantalGoed
MailTekst = MailTekst & BestandGoed(i) & "<br>"
Next
MailTekst = MailTekst & "<br>Niet verwerkte bestanden :<br>"
i = 1
For i = 1 To AantalFout
MailTekst = MailTekst & BestandFout(i) & "<br>"
Next
End If
End If
'Verstuur mail
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SRV0001"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
Application.ScreenUpdating = False
With iMsg
Set .Configuration = iConf
.to = "[email-adres]"
.From = "OptiDataPrep"
.Subject = MailOnderwerp
.HTMLBody = MailTekst
.Send
End With
Application.ScreenUpdating = True
Set iMsg = Nothing
Set iConf = Nothing
Application.Quit
End If
End Sub