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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| Sub MainProcedure()
Dim thisWorkbookPath As String
thisWorkbookPath = Application.ActiveWorkbook.Path
Call ExportOutlookEmailsToCSV(thisWorkbookPath)
End Sub
Sub ExportOutlookEmailsToCSV(thisWorkbookPath As String)
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olMail As Outlook.mailItem
Dim xlApp As Excel.Application
Dim xlQueryWb As Excel.Workbook
Dim xlQueryWs As Excel.Worksheet
Dim cell As Excel.Range
Dim searchWord As String
Dim csvFilePath As String
Dim csvFile As Integer
Dim outputLine As String
' Initialisation de l'application Outlook
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If olApp Is Nothing Then
Set olApp = New Outlook.Application
End If
On Error GoTo 0
' Initialisation du Namespace et du dossier Inbox
Set olNs = olApp.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(olFolderInbox) ' Inbox
' Initialisation de l'application Excel et ouverture du fichier "requête Outlook"
Set xlApp = New Excel.Application
xlApp.Visible = False
' Vérification de l'existence du fichier "requête Outlook.xlsx"
If Dir(thisWorkbookPath & "C:\VBA\requête Outlook.xlsx") = "" Then
MsgBox "Le fichier 'requête Outlook.xlsx' n'a pas été trouvé.", vbExclamation
Exit Sub
End If
' Ouverture du fichier "requête Outlook"
Set xlQueryWb = xlApp.Workbooks.Open(thisWorkbookPath & "C:\VBA\requête Outlook.xlsx")
Set xlQueryWs = xlQueryWb.Sheets(1)
' Chemin du fichier CSV de sortie
csvFilePath = thisWorkbookPath & "C:\VBA\résultats Outlook.csv"
csvFile = FreeFile
' Création du fichier CSV et écriture des en-têtes
Open csvFilePath For Output As #csvFile
Print #csvFile, "mot,titre mail,corps mail,date"
' Boucle sur chaque mot de la première colonne du fichier "requête Outlook"
Dim lastRow As Long
lastRow = xlQueryWs.Cells(xlQueryWs.Rows.Count, 1).End(xlUp).Row
For Each cell In xlQueryWs.Range("A1:A" & lastRow)
If Not IsEmpty(cell.Value) Then
searchWord = cell.Value
' Recherche des mails contenant le mot
For Each olMail In olFolder.Items
If TypeName(olMail) = "MailItem" Then
If InStr(1, olMail.Subject, searchWord, vbTextCompare) > 0 Or _
InStr(1, olMail.Body, searchWord, vbTextCompare) > 0 Then
' Écriture des résultats dans le fichier CSV
outputLine = """" & searchWord & """,""" & _
Replace(olMail.Subject, """", """""") & """,""" & _
Replace(Replace(olMail.Body, vbCrLf, " "), """", """""") & """,""" & _
olMail.ReceivedTime & """"
Print #csvFile, outputLine
End If
End If
Next olMail
End If
Next cell
' Fermeture du fichier CSV et du fichier Excel "requête Outlook"
Close #csvFile
xlQueryWb.Close False
xlApp.Quit
' Nettoyage
Set olMail = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
Set xlQueryWs = Nothing
Set xlQueryWb = Nothing
Set xlApp = Nothing
MsgBox "Exportation terminée.", vbInformation
End Sub |
Partager