Function MailInlezen()
' Variabelen declareren
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Folder As Outlook.Folder
Dim Item As Outlook.MailItem
Dim Atmt As Outlook.Attachment
Dim cnt As String
Dim FileName As String, FileImport As String, tmpFileImport As String, Pad As String, tmpFile As String
Dim Mapje As String, SolNum As String
Dim i As Integer, iCount As Integer, iToe As Integer, iX As Integer, iArr As Integer
Dim arr As Variant
Dim tempi
On Error GoTo SaveAttachmentsToFolder_err
'-------------------------------------------------------------------------------------------
'Pad voor bestanden. Map wordt aangemaakt als hij niet bestaat.
'-------------------------------------------------------------------------------------------
Pad = "H:\Sollicitaties\" & Format(Date, "yyyy") & "\"
On Error GoTo DirMaken
Pad = PickFolder(Pad)
ChangeFolder:
ChDir (Pad)
Set DestWB = Workbooks(WerkBoek)
Set Overzicht = DestWB.Sheets(WerkBlad)
'-------------------------------------------------------------------------------------------
'Met Late binding een Outlook sessie openen.
'-------------------------------------------------------------------------------------------
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number = 429 Then
Set olApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0
'-------------------------------------------------------------------------------------------
' De variabele Folder instellen op de standaard postbus
'-------------------------------------------------------------------------------------------
Set olNs = olApp.GetNamespace("MAPI")
Set Folder = olNs.PickFolder
'Voorbeeld van uitlezen van toegevoegde postbus
''Set Folder = olNs.Folders("Naam van de postbus").Folders("Postvak in").Folders("Sollicitaties")
'Voorbeeld van uitlezen van mailfolder in eigen postbus
''Set Folder = olNs.GetDefaultFolder(olFolderInbox).Folders("Sollicitaties") ' Early Binding
''Set Folder = olNs.GetDefaultFolder(6).Folders("Sollicitaties") ' Late Binding
'-------------------------------------------------------------------------------------------
' Controleer de Folder op mails en sluit af als er niets gevonden wordt.
'-------------------------------------------------------------------------------------------
i = 0
If Folder.Items.Count = 0 Then
MsgBox "Er zijn geen sollicitaties in de folder " & Folder.Name & " gevonden.", vbInformation, "Niets gevonden"
Exit Function
End If
iCount = Folder.Items.Count
i = iCount - 1
'-----------------------------------------------------------------------------------------
' Door alle mails lopen en uitlezen. Bijlagen opslaan in aparte map.
'-----------------------------------------------------------------------------------------
For Each Item In Folder.Items
cont = Split(Item.Body, vbCrLf)
arr = cont
myArray = Overzicht.UsedRange
'-------------------------------------------------------------------------------------
' Laatste rij vinden om records op te slaan.
'-------------------------------------------------------------------------------------
With Overzicht
.Activate
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
Cells(LastRow, 1).Select
'-------------------------------------------------------------------------------------
' Door array lopen om startpositie te vinden.
'-------------------------------------------------------------------------------------
For i = LBound(arr) To UBound(arr)
If LCase(Left(arr(i), 26)) = "solliciteren bij amsterdam" Then
iRij = i + 1
Exit For
End If
Next
On Error Resume Next
'-------------------------------------------------------------------------------------
' Sollicitatienummer in werkblad zetten.
'-------------------------------------------------------------------------------------
Cells(LastRow + 1, 1) = arr(iRij)
x = 1
SolNum = arr(iRij)
'-------------------------------------------------------------------------------------
' Eerste blok naamgegevens uitlezen. Tussenvoegsel elimineren.
'-------------------------------------------------------------------------------------
For i = 3 To 8 Step 2
iArr = i
x = x + 1
If LCase(Left(arr(i + 1), 13)) = "tussenvoegsel" Then
Cells(LastRow + 1, x) = StrConv(arr(iArr), vbProperCase) & ", " _
& LCase(arr(iArr + 2))
iArr = iArr + 2
Else
If i = 3 Then
Cells(LastRow + 1, x) = UCase(arr(iArr))
Else
Cells(LastRow + 1, x) = StrConv(arr(iArr), vbProperCase)
End If
End If
Next i
'-------------------------------------------------------------------------------------
' Bullets uitlezen.
'-------------------------------------------------------------------------------------
x = x + 1
iArr = iArr + 3
Cells(LastRow + 1, x) = StrConv(arr(iArr), vbProperCase)
'-------------------------------------------------------------------------------------
' Geboortedatum uitlezen.
'-------------------------------------------------------------------------------------
If LCase(Left(arr(iArr + 2), 13)) = "geboortedatum" Then
x = x + 1
iArr = iArr + 3
Cells(LastRow + 1, x) = arr(iArr)
iArr = iArr + 3
Else
x = x + 1
iArr = iArr + 4
End If
'-------------------------------------------------------------------------------------
' Adresgegevens 1 uitlezen. Straat, Huisnummer + toevoeging
'-------------------------------------------------------------------------------------
iArr = iArr + 3
For i = (iArr) To (iArr + 2) Step 2
x = x + 1
''iArr = iArr + 2
If LCase(arr(i - 1)) = "huisnummer" Then
If LCase(arr(i + 1)) = "huisnummer toevoeging" Then
Cells(LastRow + 1, x) = arr(i) & ", " & UCase(arr(i + 2))
iArr = iArr + 2
Else
Cells(LastRow + 1, x) = StrConv(arr(i), vbProperCase)
End If
ElseIf LCase(arr(i - 1)) = "huisnummer toevoeging" Then
Else
Cells(LastRow + 1, x) = StrConv(arr(i), vbProperCase)
End If
Next i
'-------------------------------------------------------------------------------------
' Adresgegevens 2 uitlezen. Woonplaats, Postcode, Email, Telefoon
'-------------------------------------------------------------------------------------
iArr = iArr + 4
For i = (iArr) To (iArr + 7) Step 2
x = x + 1
Cells(LastRow + 1, x) = StrConv(arr(i), vbProperCase)
Next i
'-------------------------------------------------------------------------------------
' Bijlagen uitlezen
'-------------------------------------------------------------------------------------
iArr = iArr + 3
For i = iArr To iArr + 2 Step 2
On Error Resume Next
x = x + 1
Cells(LastRow + 1, x) = StrConv(arr(i), vbProperCase)
Next i
'-------------------------------------------------------------------------------------
'Medium vraag (facultatief) LET OP i WAARDE!!!
'-------------------------------------------------------------------------------------
iArr = i + 1
If LCase(Left(arr(iArr - 2), 15)) = "via welk medium" Then
x = x + 1
Cells(LastRow + 1, x) = StrConv(arr(iArr), vbProperCase)
End If
'-------------------------------------------------------------------------------------
' Bijlagen opslaan in eigen map met naam sollicitant
'-------------------------------------------------------------------------------------
For Each Atmt In Item.Attachments
iToe = 0
Mapje = Pad & IIf(Right(Pad, 1) <> "\", "\", "") & SolNum & "\" & arr(7) & "\"
On Error GoTo MapPersoon
PersoonMapGemaakt:
If Dir(Mapje, vbDirectory) & "" = "" Then CreateDir (Mapje)
FileName = Mapje & Atmt.FileName
'-----------------------------------------------------------------------------
'Eerst controleren of het importbestand al in de map Import staat...
'-----------------------------------------------------------------------------
tmpFile = Dir(FileName)
If tmpFile & "" = "" Then
'-------------------------------------------------------------------------
'Bestand is nog niet opgeslagen; opslaan.
'-------------------------------------------------------------------------
Atmt.SaveAsFile FileName
Else
'-------------------------------------------------------------------------
'Bestand(snaam) bestaat al; opslaan onder een nieuwe naam.
'-------------------------------------------------------------------------
iToe = iToe + 1
tmp = Split(tmpFile, ".")
FileName = ""
For i = LBound(tmp) To UBound(tmp) - 1
If Not FileName = "" Then FileName = FileName & "."
FileName = FileName & tmp(i)
Next i
FileName = FileName & "_" & iToe & "." & tmp(UBound(tmp))
Atmt.SaveAsFile Mapje & FileName
End If
Next Atmt
Next Item
' Maak het geheugen leeg
SaveAttachmentsToFolder_exit:
Set olNs = Nothing
Set olApp = Nothing
Set Atmt = Nothing
Set Item = Nothing
Set NS = Nothing
Exit Function
SaveAttachmentsToFolder_err:
'----------------------------------------------------------
' Foutverwerking
'----------------------------------------------------------
MsgBox "Er is een fout opgetreden." & vbCrLf _
& "Error Nummer: " & Err.Number & vbCrLf _
& "Error omschrijving: " _
& Err.Description, vbCritical, "Foutje!"
Resume SaveAttachmentsToFolder_exit
DirMaken:
'----------------------------------------------------------
'Nieuwe map maken als hij nog niet bestaat
'----------------------------------------------------------
Call CreateDir(Pad)
GoTo ChangeFolder
MapPersoon:
Call CreateDir(Pad)
GoTo PersoonMapGemaakt
End Function