Dim FileName As String
Dim ns As outlook.Namespace
Dim inbox As outlook.MAPIFolder
Dim atmt As outlook.Attachment
Dim i As Integer, x As Integer, k As Integer, NbMess As Integer
Sub OutlookExtractorProcessus()
Application.Calculation = xlCalculationAutomatic
Application.EnableCancelKey = xlDisabled
Sheets("Menu").Select
ProcessorName = ActiveWorkbook.Name
Application.DisplayStatusBar = True
Application.StatusBar = "Ready"
Call Getmailattachements
Application.ScreenUpdating = True
Workbooks(ProcessorName).Save
Application.StatusBar = "Ready"
Application.EnableCancelKey = xlEnabled
End Sub
Sub Getmailattachements()
Dim i As Integer
Dim osender As String, TempOsender As String, Osign As String
Dim fs, f
Dim gs, g, s
Dim fsa
Dim ArchiveFolder As String, ArchiveNameTag As String
Dim Oitem As outlook.MailItem
Dim NbCaract As Integer, NbCaractNsender As Integer, NbCaractStesender As Integer
Dim OfileName As String, OextFile As String, OfilePath As String
Dim OfileDateRecept As Variant, ItemValue As Variant
Dim MailDateRecept As Date
Dim OSubject As String
Dim atmtCount As Integer
ProcessorName = ActiveWorkbook.Name
statusBarInitial = Application.DisplayStatusBar
Application.DisplayStatusBar = True
Application.ScreenUpdating = False
ArchiveFolder = Workbooks(ProcessorName).Sheets("Menu").Cells(7, 5) & "\"
WorkFilePath = Workbooks(ProcessorName).Sheets("Menu").Cells(8, 5) & "\"
On Error GoTo Terminer:
Set ns = outlook.GetNamespace("MAPI")
Set inbox = ns.Folders("Mailbox - Shirley Yeung").Folders("zzzz")
On Error GoTo 0
x = 0
k = inbox.Items.Count
NbMess = k
If k = 0 Then
'MsgBox "There are no messages in the selected Mailbox !", vbInformation, "Nothing Found"
GoTo Terminer
End If
For i = 1 To k
If k <= 0 Then
GoTo Terminer
End If
Application.StatusBar = "/!\ Emails detachment running... Email : " & i & " of " & NbMess
On Error GoTo Terminer
Set Oitem = inbox.Items(k)
On Error GoTo 0
osender = Oitem.SenderEmailAddress
OSubject = Oitem.Subject
'Remove forbidden characters from filename
OSubject = Replace(OSubject, "\", " ")
OSubject = Replace(OSubject, "/", " ")
OSubject = Replace(OSubject, ">", " ")
OSubject = Replace(OSubject, "<", " ")
OSubject = Replace(OSubject, ",", " ")
OSubject = Replace(OSubject, ";", " ")
OSubject = Replace(OSubject, ":", " ")
OSubject = Replace(OSubject, "?", " ")
OSubject = Replace(OSubject, "*", " ")
OSubject = Replace(OSubject, Chr(34), " ")
OSubject = Left(OSubject, 120)
MailDateRecept = Oitem.ReceivedTime
lannee = Year(MailDateRecept)
lemois = Month(MailDateRecept)
If lemois < 10 Then lemois = "0" & lemois
lejour = Day(MailDateRecept)
If lejour < 10 Then lejour = "0" & lejour
lheure = Hour(MailDateRecept)
If lheure < 10 Then lheure = "0" & lheure
lesminutes = Minute(MailDateRecept)
If lesminutes < 10 Then lesminutes = "0" & lesminutes
lsecond = Second(MailDateRecept)
If lsecond < 10 Then lsecond = "0" & lsecond
OfileDateRecept = "_" & lannee & lemois & lejour & "_" & lheure & "h" & lesminutes & "m"
Osign = "@"
NbCaract = Len(osender)
NbCaractNsender = InStr([osender], [Osign])
NbCaractStesender = (NbCaract - NbCaractNsender)
TempOsender = Right(osender, NbCaractStesender)
Workbooks(ProcessorName).Activate
Sheets("XrefEmail").Select
[A1].Select
Range("K2").Select
Dim subjectVal As String
On Error Resume Next
For Each A In Range(Selection, Selection.End(xlDown))
subjectVal = A.Value
If InStr(1, OSubject, subjectVal, 1) Then
ActiveCell.Offset(0, -7).Range("A1").Select
ArchiveNameTag = ActiveCell.Text
OfilePath = ArchiveFolder & lannee & "-" & lemois & "\" & ArchiveNameTag & "\"
'Go through attachments
For Each atmt In Oitem.Attachments
DoEvents
OfileName = atmt.FileName
OextFile = Right(OfileName, 4)
If OextFile = ".csv" Then
OextFile = ".txt"
ElseIf OextFile = ".CSV" Then
OextFile = ".txt"
ElseIf OextFile = ".xlt" Then
OextFile = ".xls"
ElseIf OextFile = "xlsx" Then
OextFile = ".xlsx"
End If
Set fsa = CreateObject("Scripting.FileSystemObject")
If fsa.FileExists(WorkFilePath & ArchiveNameTag & OfileDateRecept & "_" & OfileName & OextFile) Then
Randomize (1000)
OfileName = OfileName & Rnd(1000)
End If
OfileName = OfileDateRecept & "_" & OfileName & OextFile
atmt.SaveAsFile WorkFilePath & ArchiveNameTag & OfileName
atmtCount = atmtCount + 1
Next atmt
'Check if archive folder does already exists
Set gs = CreateObject("Scripting.FileSystemObject")
Set g = gs.GetFolder(OfilePath)
s = g.DateCreated
If s > 1 Then
'Save email in the relvant archive folder
Oitem.SaveAs OfilePath & ArchiveNameTag & OfileDateRecept & lsecond & "s_" & OSubject & ".msg", olMSGUnicode
Else
Set g = gs.GetFolder(ArchiveFolder & lannee & "-" & lemois)
s = g.DateCreated
If Not s > 1 Then
MkDir (ArchiveFolder & lannee & "-" & lemois)
MkDir (ArchiveFolder & lannee & "-" & lemois & "\" & ArchiveNameTag)
Else
MkDir (ArchiveFolder & lannee & "-" & lemois & "\" & ArchiveNameTag)
End If
Oitem.SaveAs OfilePath & ArchiveNameTag & OfileDateRecept & lsecond & "s_" & OSubject & ".msg", olMSGUnicode
End If
ActiveCell.Offset(0, 1).FormulaR1C1 = osender
ActiveCell.Offset(0, 2).FormulaR1C1 = OSubject
ActiveCell.Offset(0, 3).FormulaR1C1 = "'" & lejour & "/" & lemois & "/" & lannee & " " & lheure & ":" & lesminutes
'Deletes Email if its contains an attachment. Otherwise it keeps the Email in inbox and goes to the next email.
If atmtCount >= 0 Then
Oitem.Delete
CountEmails = CountEmails + 1
Else
x = x + 1
End If
End If
Next A
x = x + 1
OfileName = ""
OextFile = ""
OfilePath = ""
ArchiveNameTag = ""
TempOsender = ""
OSubject = ""
NbCaract = "0"
NbCaractNsender = "0"
NbCaractStesender = "0"
OfileDateRecept = ""
MailDateRecept = "0"
atmtCount = 0
g = "0"
gs = "0"
s = "0"
k = inbox.Items.Count
k = k - x
Next
Terminer:
Sheets("Menu").Select
Application.StatusBar = "Ready"
End Sub