Passer au contenu principal

Comment enregistrer toutes les pièces jointes de plusieurs e-mails dans un dossier dans Outlook?

Il est facile d'enregistrer toutes les pièces jointes d'un e-mail avec la fonctionnalité intégrée Enregistrer toutes les pièces jointes dans Outlook. Cependant, si vous souhaitez enregistrer toutes les pièces jointes de plusieurs e-mails à la fois, aucune fonctionnalité directe ne peut vous aider. Vous devez appliquer à plusieurs reprises la fonction Enregistrer toutes les pièces jointes dans chaque e-mail jusqu'à ce que toutes les pièces jointes soient enregistrées à partir de ces e-mails. Cela prend du temps. Dans cet article, nous présentons deux méthodes pour enregistrer facilement toutes les pièces jointes de plusieurs e-mails dans un dossier spécifique dans Outlook.

Enregistrez toutes les pièces jointes de plusieurs e-mails dans un dossier avec le code VBA
Plusieurs clics pour enregistrer toutes les pièces jointes de plusieurs e-mails dans un dossier avec un outil incroyable


Enregistrez toutes les pièces jointes de plusieurs e-mails dans un dossier avec le code VBA

Cette section présente un code VBA dans un guide étape par étape pour vous aider à enregistrer rapidement toutes les pièces jointes de plusieurs e-mails dans un dossier spécifique à la fois. Veuillez faire comme suit.

1. Tout d'abord, vous devez créer un dossier pour enregistrer les pièces jointes sur votre ordinateur.

Entrez dans le Documents dossier et créez un dossier nommé «Pièces jointes». Voir capture d'écran:

2. Sélectionnez les e-mails dont vous allez enregistrer les pièces jointes, puis appuyez sur autre + F11 clés pour ouvrir le Microsoft Visual Basic pour applications fenêtre.

3. Cliquez insérer > Module ouvrir le Module fenêtre, puis copiez l'un des codes VBA suivants dans la fenêtre.

Code VBA 1: enregistrer en masse les pièces jointes de plusieurs e-mails (enregistrer exactement les pièces jointes du même nom directement)

Pourboires: Ce code enregistrera exactement les pièces jointes du même nom en ajoutant les chiffres 1, 2, 3 ... après les noms de fichiers.

Dim GCount As Integer
Dim GFilepath As String
Public Sub SaveAttachments()
'Update 20200821
Dim xMailItem As Outlook.MailItem
Dim xAttachments As Outlook.Attachments
Dim xSelection As Outlook.Selection
Dim i As Long
Dim xAttCount As Long
Dim xFilePath As String, xFolderPath As String, xSaveFiles As String
On Error Resume Next
xFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
Set xSelection = Outlook.Application.ActiveExplorer.Selection
xFolderPath = xFolderPath & "\Attachments\"
If VBA.Dir(xFolderPath, vbDirectory) = vbNullString Then
    VBA.MkDir xFolderPath
End If
GFilepath = ""
For Each xMailItem In xSelection
    Set xAttachments = xMailItem.Attachments
    xAttCount = xAttachments.Count
    xSaveFiles = ""
    If xAttCount > 0 Then
        For i = xAttCount To 1 Step -1
            GCount = 0
            xFilePath = xFolderPath & xAttachments.Item(i).FileName
            GFilepath = xFilePath
            xFilePath = FileRename(xFilePath)
            If IsEmbeddedAttachment(xAttachments.Item(i)) = False Then
                xAttachments.Item(i).SaveAsFile xFilePath
                If xMailItem.BodyFormat <> olFormatHTML Then
                    xSaveFiles = xSaveFiles & vbCrLf & "<Error! Hyperlink reference not valid.>"
                Else
                    xSaveFiles = xSaveFiles & "<br>" & "<a href='file://" & xFilePath & "'>" & xFilePath & "</a>"
                End If
            End If
        Next i
    End If
Next
Set xAttachments = Nothing
Set xMailItem = Nothing
Set xSelection = Nothing
End Sub

Function FileRename(FilePath As String) As String
Dim xPath As String
Dim xFso As FileSystemObject
On Error Resume Next
Set xFso = CreateObject("Scripting.FileSystemObject")
xPath = FilePath
FileRename = xPath
If xFso.FileExists(xPath) Then
    GCount = GCount + 1
    xPath = xFso.GetParentFolderName(GFilepath) & "\" & xFso.GetBaseName(GFilepath) & " " & GCount & "." + xFso.GetExtensionName(GFilepath)
    FileRename = FileRename(xPath)
End If
xFso = Nothing
End Function

Function IsEmbeddedAttachment(Attach As Attachment)
Dim xItem As MailItem
Dim xCid As String
Dim xID As String
Dim xHtml As String
On Error Resume Next
IsEmbeddedAttachment = False
Set xItem = Attach.Parent
If xItem.BodyFormat <> olFormatHTML Then Exit Function
xCid = ""
xCid = Attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F")
If xCid <> "" Then
    xHtml = xItem.HTMLBody
    xID = "cid:" & xCid
    If InStr(xHtml, xID) > 0 Then
        IsEmbeddedAttachment = True
    End If
End If
End Function
Code VBA 2: enregistrer en bloc les pièces jointes de plusieurs e-mails (vérifier les doublons)
Public Sub SaveAttachments()
'Update 20200821
Dim xMailItem As Outlook.MailItem
Dim xAttachments As Outlook.Attachments
Dim xSelection As Outlook.Selection
Dim i As Long
Dim xAttCount As Long
Dim xFilePath As String, xFolderPath As String, xSaveFiles As String
Dim xYesNo As Integer
Dim xFlag As Boolean
On Error Resume Next
xFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
Set xSelection = Outlook.Application.ActiveExplorer.Selection
xFolderPath = xFolderPath & "\Attachments\"
If VBA.Dir(xFolderPath, vbDirectory) = vbNullString Then
    VBA.MkDir xFolderPath
End If
For Each xMailItem In xSelection
    Set xAttachments = xMailItem.Attachments
    xAttCount = xAttachments.Count
    xSaveFiles = ""
    If xAttCount > 0 Then
        For i = xAttCount To 1 Step -1
            xFilePath = xFolderPath & xAttachments.Item(i).FileName
            xFlag = True
            If VBA.Dir(xFilePath, 16) <> Empty Then
                xYesNo = MsgBox("The file is exists, do you want to replace it", vbYesNo + vbInformation, "Kutools for Outlook")
                If xYesNo = vbNo Then xFlag = False
            End If
            If xFlag = True Then
                xAttachments.Item(i).SaveAsFile xFilePath
                If xMailItem.BodyFormat <> olFormatHTML Then
                    xSaveFiles = xSaveFiles & vbCrLf & "<Error! Hyperlink reference not valid.>"
                Else
                    xSaveFiles = xSaveFiles & "<br>" & "<a href='file://" & xFilePath & "'>" & xFilePath & "</a>"
                End If
            End If
        Next i
    End If
Next
Set xAttachments = Nothing
Set xMailItem = Nothing
Set xSelection = Nothing
End Sub

Notes:

1) Si vous souhaitez enregistrer toutes les pièces jointes de même nom dans un dossier, veuillez appliquer ce qui précède Code VBA 1. Avant d'exécuter ce code, veuillez cliquer sur Outils > Bibliographie, puis vérifiez le Exécution de scripts Microsoft boîte dans la Références - Projet boite de dialogue;

doc enregistrer les pièces jointes07

2) Si vous souhaitez vérifier les noms de pièces jointes en double, veuillez appliquer le code VBA 2. Après avoir exécuté le code, une boîte de dialogue apparaîtra pour vous rappeler s'il faut remplacer les pièces jointes en double, choisissez Oui or Non en fonction de vos besoins.

5. appuie sur le F5 clé pour exécuter le code.

Ensuite, toutes les pièces jointes des e-mails sélectionnés sont enregistrées dans le dossier que vous avez créé à l'étape 1. 

Notes: Il peut y avoir un Microsoft Outlook boîte de dialogue qui apparaît, veuillez cliquer sur le Autoriser bouton pour continuer.


Enregistrez toutes les pièces jointes de plusieurs e-mails dans un dossier avec un outil incroyable

Si vous êtes un débutant en VBA, voici fortement recommandé le Enregistrer toutes les pièces jointes utilité de Kutools pour Outook pour vous. Avec cet utilitaire, vous pouvez enregistrer rapidement toutes les pièces jointes de plusieurs e-mails à la fois en plusieurs clics uniquement dans Outlook.
Avant d'appliquer la fonctionnalité, veuillez téléchargez et installez d'abord Kutools pour Outlook.

1. Sélectionnez les e-mails contenant les pièces jointes que vous souhaitez enregistrer.

Conseils: Vous pouvez sélectionner plusieurs e-mails non adjacents en maintenant le bouton Ctrl touchez et sélectionnez-les un par un;
Ou sélectionnez plusieurs e-mails adjacents en maintenant le Shift et sélectionnez le premier e-mail et le dernier.

2. Cliquez Kutools >Outils de fixationEnregistrer tout. Voir la capture d'écran:

3. dans le Enregistrer les paramètres dialogue, cliquez sur pour sélectionner un dossier pour enregistrer les pièces jointes, puis cliquez sur le OK .

3. Cliquez OK deux fois dans la boîte de dialogue suivante, puis toutes les pièces jointes des e-mails sélectionnés sont enregistrées dans le dossier spécifié à la fois.

Notes:

  • 1. Si vous souhaitez enregistrer des pièces jointes dans différents dossiers en fonction des e-mails, veuillez vérifier la Créez des sous-dossiers dans le style suivant et choisissez un style de dossier dans la liste déroulante.
  • 2. En plus de sauvegarder toutes les pièces jointes, vous pouvez enregistrer les pièces jointes selon des conditions spécifiques. Par exemple, vous ne souhaitez enregistrer que les pièces jointes PDF dont le nom de fichier contient le mot "Facture", veuillez cliquer sur le bouton Options avancées pour étendre les conditions, puis configurer comme le screebshot ci-dessous montré.
  • 3. Si vous souhaitez enregistrer automatiquement les pièces jointes à l'arrivée des e-mails, le Enregistrer automatiquement les pièces jointes la fonctionnalité peut vous aider.
  • 4. Pour détacher les pièces jointes directement des e-mails sélectionnés, le Détacher toutes les pièces jointes caractéristique de Kutools for Outlook peut vous faire une faveur.

  Si vous souhaitez bénéficier d'un essai gratuit (60 jours) de cet utilitaire, veuillez cliquer pour le télécharger, puis passez à appliquer l'opération selon les étapes ci-dessus.


Articles connexes

Insérer des pièces jointes dans le corps du message électronique dans Outlook
Normalement, les pièces jointes sont affichées dans le champ Attaché dans un e-mail de composition. Ici, ce didacticiel fournit des méthodes pour vous aider à insérer facilement des pièces jointes dans le corps de l'e-mail dans Outlook.

Télécharger / enregistrer automatiquement les pièces jointes à partir d'Outlook dans un certain dossier
De manière générale, vous pouvez enregistrer toutes les pièces jointes d'un e-mail en cliquant sur Pièces jointes> Enregistrer toutes les pièces jointes dans Outlook. Mais, si vous avez besoin de sauvegarder toutes les pièces jointes de tous les e-mails reçus et reçus, l'idéal? Cet article présentera deux solutions pour télécharger automatiquement les pièces jointes à partir d'Outlook vers un certain dossier.

Imprimer toutes les pièces jointes dans un / plusieurs e-mails dans Outlook
Comme vous le savez, il n'imprimera que le contenu de l'e-mail tel que l'en-tête, le corps lorsque vous cliquez sur Fichier> Imprimer dans Microsoft Outlook, mais n'imprimera pas les pièces jointes. Ici, nous allons vous montrer comment imprimer facilement toutes les pièces jointes dans un e-mail sélectionné dans Microsoft Outlook.

Rechercher des mots dans la pièce jointe (contenu) dans Outlook
Lorsque nous tapons un mot-clé dans la zone de recherche instantanée d'Outlook, il recherchera le mot-clé dans les sujets, le corps, les pièces jointes des e-mails, etc. Cet article vous montre les étapes détaillées pour rechercher facilement des mots dans le contenu des pièces jointes dans Outlook.

Conserver les pièces jointes lors de la réponse dans Outlook
Lorsque nous transférons un message électronique dans Microsoft Outlook, les pièces jointes d'origine de ce message électronique sont conservées dans le message transféré. Cependant, lorsque nous répondons à un e-mail, les pièces jointes d'origine ne seront pas jointes au nouveau message de réponse. Ici, nous allons présenter quelques astuces pour conserver les pièces jointes d'origine lors de la réponse dans Microsoft Outlook.


Meilleurs outils de productivité bureautique

Kutools for Outlook - Plus de 100 fonctionnalités puissantes pour booster votre Outlook

🤖 Assistant de messagerie IA: E-mails professionnels instantanés avec la magie de l'IA : un clic pour des réponses géniales, un ton parfait, une maîtrise multilingue. Transformez l’emailing sans effort ! ...

📧 Email Automation: Absent du bureau (disponible pour POP et IMAP)  /  Programmer l'envoi d'e-mails  /  Auto CC/BCC par règles lors de l'envoi d'un e-mail  /  Transfert automatique (règles avancées)   /  Ajouter un message d'accueil automatique   /  Divisez automatiquement les e-mails multi-destinataires en messages individuels 

(I.e. Email Management: Rappel facile des e-mails  /  Bloquer les e-mails frauduleux par sujets et autres  /  Supprimer les e-mails en double  /  Recherche Avancée  /  Consolider les dossiers 

(I.e. Pièces jointes ProSauvegarde par lots  /  Détachement par lots  /  Compression par lots  /  Enregistrement automatique   /  Détachement automatique  /  Compression automatique 

???? Magie de l'interface: 😊Plus d'émojis jolis et cool   /  Boostez votre productivité Outlook avec des vues à onglets  /  Réduire Outlook au lieu de fermer 

???? Merveilles en un clic: Répondre à tous avec les pièces jointes entrantes  /   E-mails anti-hameçonnage  /  🕘Afficher le fuseau horaire de l'expéditeur 

👩🏼‍🤝‍👩🏻 Contacts et calendrier: Ajouter par lots des contacts à partir des e-mails sélectionnés  /  Diviser un groupe de contacts en groupes individuels  /  Supprimer les rappels d'anniversaire 

infos Caractéristiques 100 Attendez votre exploration ! Cliquez ici pour en savoir plus.

 

 

Comments (81)
Rated 3.5 out of 5 · 3 ratings
This comment was minimized by the moderator on the site
Thank you for sharing the code. Unfortunately, I tried both with failure. This is what I got - The macros in this project are disabled. Please refer to the online help or documentation of the host application to determine how to enable macros. Thank you.
This comment was minimized by the moderator on the site
Hi,
Please follow the instructions in the screenshot below to check if macros are enabled in the macro settings in your Outlook. After enabling both options, re-run the VBA code.

https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/macro-enabled.png
This comment was minimized by the moderator on the site
Thank you so much.
Rated 5 out of 5
This comment was minimized by the moderator on the site
Thank you for sharing VBA code. This work like magic and is going to save it lots of time!
This comment was minimized by the moderator on the site
Hello friends!

Thanks for sharing this VBA code.

Is there any way to change the location of the save folder?

I share the pc with some colleagues and in this case I need the files to be saved in a password protected folder which is not located in the documents folder.

How can I make this change?

Thank you in advance
This comment was minimized by the moderator on the site
Hi Fabiana,
Change the line 14
xFolderPath = xFolderPath & "\Attachments\"

to
xFolderPath = "C:\Users\Win10x64Test\Desktop\save attachments\1\"

Here "C:\Users\Win10x64Test\Desktop\save attachments\1\" is the folder path in my case.
Don't forget to end the folder path with a slash "\"
This comment was minimized by the moderator on the site
Hello friends!

Thank you for sharing that VBA code.

Is there any way to change the location of the save folder?

I share the pc with some colleagues and in this case I need the files to be saved in a password protected folder which is not located in the documents folder.

How can I make this change?

Thank you in advance
This comment was minimized by the moderator on the site
If you are trying to run the Code that renames duplicate files and keep getting a "User Type Not Defined" error message here is the code fixed. Instead of the "Dim xFso As FileSystemObject" on line 47 it should be "Dim xFso As Variant"
Also added a Message Box to appear at the end of data transfer.

Dim GCount As Integer
Dim GFilepath As String
Public Sub SaveAttachments()
'Update 20200821
Dim xMailItem As Outlook.MailItem
Dim xAttachments As Outlook.Attachments
Dim xSelection As Outlook.Selection
Dim i As Long
Dim xAttCount As Long
Dim xFilePath As String, xFolderPath As String, xSaveFiles As String
On Error Resume Next
xFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
Set xSelection = Outlook.Application.ActiveExplorer.Selection
xFolderPath = xFolderPath & "\Attachments\"
If VBA.Dir(xFolderPath, vbDirectory) = vbNullString Then
VBA.MkDir xFolderPath
End If
GFilepath = ""
For Each xMailItem In xSelection
Set xAttachments = xMailItem.Attachments
xAttCount = xAttachments.Count
xSaveFiles = ""
If xAttCount > 0 Then
For i = xAttCount To 1 Step -1
GCount = 0
xFilePath = xFolderPath & xAttachments.Item(i).FileName
GFilepath = xFilePath
xFilePath = FileRename(xFilePath)
If IsEmbeddedAttachment(xAttachments.Item(i)) = False Then
xAttachments.Item(i).SaveAsFile xFilePath
If xMailItem.BodyFormat <> olFormatHTML Then
xSaveFiles = xSaveFiles & vbCrLf & "<Error! Hyperlink reference not valid.>"
Else
xSaveFiles = xSaveFiles & "<br>" & "<a href='file://" & xFilePath & "'>" & xFilePath & "</a>"
End If
End If
Next i
End If
Next
Set xAttachments = Nothing
Set xMailItem = Nothing
Set xSelection = Nothing
MsgBoX prompt:="File Transfer Complete", Title:="Sweatyjalapenos tha Goat"
End Sub

Function FileRename(FilePath As String) As String
Dim xPath As String
Dim xFso As Variant
On Error Resume Next
Set xFso = CreateObject("Scripting.FileSystemObject")
xPath = FilePath
FileRename = xPath
If xFso.FileExists(xPath) Then
GCount = GCount + 1
xPath = xFso.GetParentFolderName(GFilepath) & "\" & xFso.GetBaseName(GFilepath) & " " & GCount & "." + xFso.GetExtensionName(GFilepath)
FileRename = FileRename(xPath)
End If
xFso = Nothing
End Function

Function IsEmbeddedAttachment(Attach As Attachment)
Dim xItem As MailItem
Dim xCid As String
Dim xID As String
Dim xHtml As String
On Error Resume Next
IsEmbeddedAttachment = False
Set xItem = Attach.Parent
If xItem.BodyFormat <> olFormatHTML Then Exit Function
xCid = ""
xCid = Attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F")
If xCid <> "" Then
xHtml = xItem.HTMLBody
xID = "cid:" & xCid
If InStr(xHtml, xID) > 0 Then
IsEmbeddedAttachment = True

End If
End If
End Function
This comment was minimized by the moderator on the site
Very nice script as of 2022-10-19 works great, for me doesn't seem to change original message by adding text. The only thing I changed is I added message received date time to each file name with the following format so it would nicely sort by date time in Windows folder: "yyyy-mm-dd HH-mm-ss ".

Code:

Dim GCount As Integer
Dim GFilepath As String
Public Sub SaveAttachments()
'Update 20200821
Dim xMailItem As Outlook.MailItem
Dim xAttachments As Outlook.Attachments
Dim xSelection As Outlook.Selection
Dim i As Long
Dim xAttCount As Long
Dim xFilePath As String, xFolderPath As String, xSaveFiles As String, xDateFormat As String
On Error Resume Next
xFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
Set xSelection = Outlook.Application.ActiveExplorer.Selection
xFolderPath = xFolderPath & "\Attachments\"
If VBA.Dir(xFolderPath, vbDirectory) = vbNullString Then
VBA.MkDir xFolderPath
End If
GFilepath = ""
For Each xMailItem In xSelection
Set xAttachments = xMailItem.Attachments
xAttCount = xAttachments.Count
xSaveFiles = ""
If xAttCount > 0 Then
For i = xAttCount To 1 Step -1
GCount = 0
xDateFormat = Format(xMailItem.ReceivedTime, "yyyy-mm-dd HH-mm-ss ")
xFilePath = xFolderPath & xDateFormat & xAttachments.Item(i).FileName
GFilepath = xFilePath
xFilePath = FileRename(xFilePath)
If IsEmbeddedAttachment(xAttachments.Item(i)) = False Then
xAttachments.Item(i).SaveAsFile xFilePath
If xMailItem.BodyFormat <> olFormatHTML Then
xSaveFiles = xSaveFiles & vbCrLf & "<Error! Hyperlink reference not valid.>"
Else
xSaveFiles = xSaveFiles & "<br>" & "<a href='file://" & xFilePath & "'>" & xFilePath & "</a>"
End If
End If
Next i
End If
Next
Set xAttachments = Nothing
Set xMailItem = Nothing
Set xSelection = Nothing
End Sub

Function FileRename(FilePath As String) As String
Dim xPath As String
Dim xFso As FileSystemObject
On Error Resume Next
Set xFso = CreateObject("Scripting.FileSystemObject")
xPath = FilePath
FileRename = xPath
If xFso.FileExists(xPath) Then
GCount = GCount + 1
xPath = xFso.GetParentFolderName(GFilepath) & "\" & xFso.GetBaseName(GFilepath) & " " & GCount & "." + xFso.GetExtensionName(GFilepath)
FileRename = FileRename(xPath)
End If
xFso = Nothing
End Function

Function IsEmbeddedAttachment(Attach As Attachment)
Dim xItem As MailItem
Dim xCid As String
Dim xID As String
Dim xHtml As String
On Error Resume Next
IsEmbeddedAttachment = False
Set xItem = Attach.Parent
If xItem.BodyFormat <> olFormatHTML Then Exit Function
xCid = ""
xCid = Attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F")
If xCid <> "" Then
xHtml = xItem.HTMLBody
xID = "cid:" & xCid
If InStr(xHtml, xID) > 0 Then
IsEmbeddedAttachment = True
End If
End If
End Function
This comment was minimized by the moderator on the site
Hi Oigo,
This is a very useful VBA script. Thank you for sharing it.
This comment was minimized by the moderator on the site
Hi crystal,

sorry for not being clear.

I was trying to use the code above mentioned. However, apparently I was doing something wrong. I was thinking that I might need to amend some parts in the code shown. For instance the path where to save the attachments and maybe some other parts. Therefore I was asking if you could share the code highlighting the parts which needs tailoring and how to tailor them.

Many thanks,
BR
This comment was minimized by the moderator on the site
Hi Rokkie,
Did you get any error prompt when the code runs? Or which line in your code is highlighted? I need more details so I can see where you can modify the code.
This comment was minimized by the moderator on the site
Hey crystal,

completeley new to this VBA. Can you share a code to use which shows where I have to amend with an example? As a Rookie it is a bit difficult to figure it out.

I am working via a Ctrix connection. Could this be a blocker for the macro?

Much appreaciate the help.
This comment was minimized by the moderator on the site
Hi Rookie,
Sorry I don't understand what you mean: "Can you share a code to use which shows where I have to amend with an example?"
And the code operates on selected emails in Outlook, Ctrix Connection does not block the macro.
This comment was minimized by the moderator on the site
Hi, I am running this Code 1 to extract .txt files from separate sub-folders of an inbox. It works great out of one sub-folder but not at all out of another sub-folder. I have tried forwarding the relevant email and attachment into other inboxes but no luck. The files are automatically generated and sent to the different sub-folders and only vary by a single letter in their title

Any help much is appreciated
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations