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 ExtractMessage()
' Variable Outlook
Dim OLapp As Outlook.Application
Dim OLspace As Outlook.Namespace
Dim OLinbox As Outlook.MAPIFolder
Dim FileName As String
' ATTENTION : variable FileName non utilisée, variable Messages utilisée mais non déclarée et non alimentée
Dim Item As Object
' Variables Fichier Texte
Dim NoFile As Integer
Dim stTextInput As String
' Variables Fichier Excel
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBook As Excel.Workbook
Set OLapp = CreateObject("Outlook.application")
Set OLspace = OLapp.GetNamespace("MAPI")
Set OLinbox = OLspace.GetDefaultFolder(olFolderInbox)
NoFile = FreeFile
Open CurrentProject.Path & "\Message" & Messages & ".txt" For Append As #NoFile
For Each Item In OLinbox.items
If Left(Item.Subject, 27) = "Objet: Demande de dépannage" Then
Print #NoFile, Item.Subject ' la date est dans l'objet du mail
Print #NoFile, Item.Body
End If
Next Item
Close #NoFile
' On ferme et ré-ouvre le fichier texte pour la lecture
Open CurrentProject.Path & "\Message" & Messages & ".txt" For Input As #NoFile
' On crée un nouveau classeur
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
xlBook.SaveAs CurrentProject.Path & "\Message" & Messages & ".xls"
' Pour un classeur existant, la syntaxe serait la suivante :
'Set xlBook = xlApp.Workbooks.Open(CurrentProject.Path & "\Message" & Messages & ".xls")
Set xlSheet = xlBook.Sheets(1)
xlSheet.Activate
' 1 création de l'entête de colonnes à remplacer par le bon nom des champs de la table
xlSheet.Cells(1, 1) = "DateDemande"
xlSheet.Cells(1, 2) = "Ville"
xlSheet.Cells(1, 3) = "Famille"
xlSheet.Cells(1, 4) = "Type"
xlSheet.Cells(1, 5) = "Description"
Do While Not EOF(NoFile)
Line Input #1, stTextInput
' 2 Extraction de la date
If Left(stTextInput, 27) = "Objet : Demande de dépannage" Then
xlSheet.Cells(2, 1) = Mid(stTextInput, 29) ' à modifier car la position exacte de la date n'est pas connue
End If
' 3 Extraction de la ville
If Left(stTextInput, 6) = "Ville:" Then
xlSheet.Cells(2, 2) = Trim(Mid(stTextInput, 7))
End If
' 4 Extraction de la famille
If Left(stTextInput, 9) = "Famille :" Then
xlSheet.Cells(2, 3) = Trim(Mid(stTextInput, 10))
End If
' 5 Extraction du type
If Left(stTextInput, 6) = "Type :" Then
xlSheet.Cells(2, 4) = Trim(Mid(stTextInput, 7))
End If
' 6 Extraction de la description
If Left(stTextInput, 25) = "Description de la panne :" Then
xlSheet.Cells(2, 5) = Trim(Mid(stTextInput, 26))
End If
Loop
Close #NoFile
xlBook.Save
xlBook.Close (True)
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
Set OLapp = Nothing
Set OLspace = Nothing
Set OLinbox = Nothing
End Sub |
Partager