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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| ' ---
' AJOUT D'UN \ EN FIN DE CHEMIN
' ---
' Entrée : strFolder <- Chemin à retraiter.
' Sortie : Chemin avec \ ajouté à la fin si nécessaire.
'
Function AddBackslash( _
ByVal strFolder As String) As String
strFolder = Trim(strFolder)
If Right(strFolder, 1) <> "\" Then strFolder = strFolder & "\"
AddBackslash = strFolder
End Function
' ---
' INSERTION DE VALEURS DANS UNE CHAINE DE CARACTERES
' ---
'
Function StringFormat( _
ByVal strChaine As String, _
ParamArray varValeurs() As Variant) As String
Dim intI As Integer
For intI = LBound(varValeurs) To UBound(varValeurs)
strChaine = Replace(strChaine, "{" & intI & "}", (varValeurs(intI)))
Next
StringFormat = strChaine
End Function
' ---
' INCREMENTATION BASIQUE D'UN NOM DE FICHIER
' ---
' Entrée : Chemin complet
' Ex. : C:\un\chemin\quelconque\test.jpg
' Sortie : Chemin incrémenté, si un fichier existe déjà à cet emplacement.
' Ex. : C:\un\chemin\quelconque\test-00001.jpg
Function FilenameInc(ByVal strFile As String) As String
Dim strFileTemp As String
Dim intI As Integer
' Si le fichier n'existe pas, on conserve son
' nom d'origine
If Dir(strFile) = "" Then
FilenameInc = strFile
Exit Function
End If
' Créer un nouveau nom de fichier numéroté,
' en vérifiant qu'il n'existe pas déjà
intI = 1
strFileTemp = strFile
While Dir(strFileTemp) <> ""
strFileTemp = StringFormat("{0}{1}-{2}.{3}", _
FilePath(strFile), _
FilenameWithoutExt(strFile), _
Format(intI, "00000"), _
FileExt(strFile))
intI = intI + 1
Wend
' Valeur de retour
FilenameInc = strFileTemp
End Function
' ---
' EXTRACTION D'UN NOM DE FICHIER SANS SON EXTENSION
' ---
' Entrée : strPath : Chemin d'un fichier
' Ex. : test.jpg
' C:\un\chemin\quelconque\test.jpg
' Sortie : Nom du fichier sans son extension (ex. : test).
'
Function FilenameWithoutExt( _
ByVal strPath As String)
Dim intI As Integer
' Extraire uniquement le nom de fichier
' (au cas où on aurait transmis un chemin complet)
strPath = Filename(strPath)
intI = InStrRev(strPath, ".", -1, vbTextCompare)
If intI = 0 Then
FilenameWithoutExt = strPath
Else
FilenameWithoutExt = Left(strPath, intI - 1)
End If
End Function
' ---
' EXTRACTION D'UN NOM DE FICHIER AVEC SON EXTENSION
' ---
' Entrée : strPath : Chemin d'un fichier
' Ex. : test.jpg
' C:\un\chemin\quelconque\test.jpg
' Sortie : Nom du fichier avec son extension (ex. : test.jpg).
'
Function Filename(ByVal strPath As String) As String
' Trouver le dernier backslash, s'il y en a un...
Dim intI As Integer
intI = InStrRev(strPath, "\", -1, vbTextCompare)
' Renvoyer la partie après le backslash
Filename = IIf(intI = 0, strPath, Mid(strPath, intI + 1))
End Function
' ---
' EXTRACTION DE L'EXTENSION D'UN FICHIER
' ---
' Entrée : strPath : Chemin d'un fichier
' Ex. : test.jpg
' C:\un\chemin\quelconque\test.jpg
' Sortie : Extension du fichier (ex. : jpg).
'
Function FileExt(ByVal strPath As String) As String
' Ne conserver que le nom de fichier
strPath = Filename(strPath)
' Trouver le dernier point
Dim intI As Integer
intI = InStrRev(strPath, ".", -1, vbTextCompare)
FileExt = IIf(intI = 0, "", Mid(strPath, intI + 1))
End Function
' ---
' EXTRACTION D'UN CHEMIN
' ---
' Entrée : strPath : Chemin d'un fichier
' Ex. : test.jpg
' C:\un\chemin\quelconque\test.jpg
' Sortie : Chemin du fichier (ex. : C:\un\chemin\quelconque\).
'
Function FilePath(ByVal strPath As String) As String
' Trouver le dernier \
Dim intI As Integer
intI = InStrRev(strPath, "\", -1, vbTextCompare)
' Valeur de retour
FilePath = IIf(intI = 0, strPath, Left(strPath, intI))
End Function
' ---
' EXTRACTION DE PIECES JOINTES OUTLOOK
' ---
'
Sub SaveAttachments( _
ByVal strTargetFolder As String, _
Optional ByVal blnIncludeSubFolders As Boolean = False)
' Quelques variables...
Dim olApp As Outlook.Application
Dim ns As Outlook.Namespace
Dim fld As Outlook.MAPIFolder
' Vérifier si le dossier de destination existe bien
If Dir(strTargetFolder, vbDirectory) = "" Then
MsgBox "Le dossier destination n'existe pas !", vbExclamation
Exit Sub
End If
strTargetFolder = AddBackslash(strTargetFolder)
' Démarrer Outlook
Set olApp = New Outlook.Application
' Pointer sur la boîte de réception
Set ns = olApp.GetNamespace("MAPI")
Set fld = ns.GetDefaultFolder(olFolderInbox)
' Extraire tous les messages du dossier
SaveFolderAttachments fld, strTargetFolder, blnIncludeSubFolders
Set fld = Nothing
Set ns = Nothing
olApp.Quit
Set olApp = Nothing
End Sub
' ---
' EXTRACTION DES PIECES JOINTES D'UN DOSSIER
' ---
'
Sub SaveFolderAttachments( _
fld As Outlook.MAPIFolder, _
strTargetFolder As String, _
Optional ByVal blnIncludeSubFolders As Boolean = False)
Dim mi As Outlook.MailItem
Dim att As Outlook.Attachment
Dim strFile As String
' Debug
Debug.Print "---"
Debug.Print "DOSSIER : " & fld.Name
Debug.Print "---"
' Parcourir tous les messages
For Each mi In fld.Items
If mi.Attachments.Count > 0 Then
' Pour info...
Debug.Print mi.Subject
For Each att In mi.Attachments
strFile = FilenameInc(strTargetFolder & att.Filename)
' Sauvegarder la pièce jointe sous son nom original
' ou avec un nom incrémenté en cas de doublons
att.SaveAsFile strFile
Debug.Print " -> " & strFile
Next
End If
Next
' Si nécessaire, effectuer le même traitement
' sur les sous-dossiers
If blnIncludeSubFolders Then
Dim subfld As Outlook.MAPIFolder
For Each subfld In fld.Folders
SaveFolderAttachments subfld, strTargetFolder, blnIncludeSubFolders
Next
End If
End Sub |
Partager