Sub Extraction_rapport() '====Déclaration Dim nomfichier Dim monMail, mesPiècesJointes As Object Dim destination As String 'destination des fichiers Dim monOutlook As New Outlook.Application Dim monEplorateur As Outlook.Explorer Dim maSelection As Outlook.Selection Dim i As Integer Dim jourdatemail, moisdatemai, annéedatemail, datemail As String Dim command '====Boîte de dialogue simple pour le chemin de sauvegarde destination = InputBox("Destination", "Sauvegarder les pièces", "C:\tempo") On Error Resume Next '====Actions sur les objets sélectionnés Set monEplorateur = monOutlook.ActiveExplorer Set maSelection = monEplorateur.Selection '====Boucle sur les mails For Each monMail In maSelection Set mesPiècesJointes = monMail.Attachments If mesPiècesJointes.Count > 0 Then '======Boucle sur les pièces jointes For i = 1 To mesPiècesJointes.Count If mesPiècesJointes(i).DisplayName "Base_Export.cab" Or mesPiècesJointes(i).DisplayName "Datalogs.cab" Then '====Verifie le nom du fichier '====Récupération de la date du rapport dans l'objet du mail jourdatemail = Left(Right(monMail.Subject, 9), 2) moisdatemail = Mid(Right(monMail.Subject, 9), 4, 2) annéedatemail = Mid(Right(monMail.Subject, 9), 7, 2) datemail = jourdatemail & "-" & moisdatemail & "-" & annéedatemail & "_" '====Sauvegarder la pièce nomfichier = datemail & mesPiècesJointes(i).DisplayName 'mesPiècesJointes(i).SaveAsFile destination & nomfichier mesPiècesJointes(i).SaveAsFile "c:\Program Files\WinZip" & nomfichier '====Dézzipper command = Shell("C:\Program Files\MacroCad\datalog\cmd.exe", 2) SendKeys "cd C:\Program Files\WinZip", True SendKeys "{ENTER}", False SendKeys "winzip32.exe -e " & nomfichier & " " & destination, False SendKeys "{ENTER}", False SendKeys "exit" + "{ENTER}", False '====Renommer le dernier fichier en fonction de datemail End If Next i End If Next Set monMail = Nothing Set mesPiècesJointes = Nothing Set monOutlook = Nothing Set monEplorateur = Nothing Set maSelection = Nothing End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPublic destination
Sub Extraction_rapport() '====Déclaration Dim monMail, mesPiècesJointes As Object Dim monOutlook As New Outlook.Application Dim monEplorateur As Outlook.Explorer Dim maSelection As Outlook.Selection Dim i As Integer Dim jourdatemail As String, moisdatemai As String, annéedatemail As String, datemail As String '====Boîte de dialogue simple pour le chemin de sauvegarde destination = InputBox("Destination", "Sauvegarder les pièces", "C:\tempo") On Error Resume Next '====Actions sur les objets sélectionnés Set monEplorateur = monOutlook.ActiveExplorer Set maSelection = monEplorateur.Selection '====Boucle sur les mails For Each monMail In maSelection Set mesPiècesJointes = monMail.Attachments If mesPiècesJointes.Count > -1 Then '======Boucle sur les pièces jointes For i = 1 To mesPiècesJointes.Count '======Récupéré les bonnes pièces jointes If mesPiècesJointes(i).DisplayName "Base_Export.cab" Or mesPiècesJointes(i).DisplayName "Datalogs.cab" Then '====Récupération de la date du rapport dans l'objet du mail jourdatemail = Left(Right(monMail.Subject, 9), 2) moisdatemail = Mid(Right(monMail.Subject, 9), 4, 2) annéedatemail = Mid(Right(monMail.Subject, 9), 7, 2) datemail = jourdatemail & "-" & moisdatemail & "-" & annéedatemail & "_" '====Sauvegarde la pièce nomfichier = datemail & mesPiècesJointes(i).DisplayName mesPiècesJointes(i).SaveAsFile destination & nomfichier End If Next i End If Next Dézzip_mail Set monMail = Nothing Set mesPiècesJointes = Nothing Set monOutlook = Nothing Set monEplorateur = Nothing Set maSelection = Nothing End Sub
Sub Dézzip_mail Dim fichier Dim rename Dim maPièce 'Emplacement et extention des fichiers adresse = destination extention = "cab" fichier = Dir(adresse & "*." & extention, vbNormal) Do While fichier <> "" If fichier <> "." And fichier <> ".." Then maPièce = fichier '====Créer le fichier *.bat qui dézippera Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.CreateTextFile(destination & "tempo.bat", True) a.writeline ("@Echo off") a.writeline ("expand -r " & "c:\tempo" & maPièce & " " & "c:\tempo") a.Close essai = Shell(destination & "tempo.bat", vbHide) '====Renome le fichier extrait du *.cab rename = Left(maPièce, Len(maPièce) - 4) maPièce = Right(rename, Len(rename) - 9) MsgBox maPièce 'C'est ici que ca plante, si j'enlève le msgbox ici(qui n'était la au départ que pour vérifier les variables), la boucle ne s'éxécute pas correctement If "Base_Export" = maPièce Then rename = rename & ".mdb" Name "c:\tempo\Base_Export.mdb" As "c:\tempo" & rename Else rename = rename & ".txt" Name "c:\tempo\Datalogs.txt" As "c:\tempo" & rename End If End If fichier = Dir Loop End Sub