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
| Sub GetXLFilesFromOutlook()
Dim oOutlk As Outlook.Application
Dim oInbox As Object, oMapi As Object
Dim mail As Object
Dim PieceJointe As Object
Dim PJindex As Integer
Set oOutlk = New Outlook.Application
Set oMapi = oOutlk.GetNamespace("MAPI")
' répertoire Inbox
Set oInbox = oMapi.GetDefaultFolder(olFolderInbox)
' boucle sur chaque mail
For Each mail In oInbox.Items
' teste la présence de pièce jointe
If mail.Attachments.count > 0 Then
Debug.Print mail.SenderName, mail.Subject, mail.Attachments.count
For PJindex = 1 To mail.Attachments.count
Set PieceJointe = mail.Attachments.Item(PJindex)
' récupère les fichiers Excel
If PieceJointe.FileName Like "*.xls" Then
PieceJointe.SaveAsFile "C:\Temp\PJOutLk" & PieceJointe.FileName
' met ton code d'importation ici
' Exemple
'DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "UneTable", _
"C:\Temp\PJOutLk" & PieceJointe.FileName
Debug.Print "Importation de " & PieceJointe.FileName
End If
Next PJindex
End If
DoEvents
Next mail
Set PieceJointe = Nothing
Set mail = Nothing
Set oInbox = Nothing
Set oOutlk = Nothing
Set oMapi = Nothing
End Sub |
Partager