Note: The other languages of the website are Google-translated. Back to English
Inloggen  \/ 
x
or
x
Registreer  \/ 
x

or

Hoe bewaar ik alle bijlagen van meerdere e-mails in een map in Outlook?

Het is gemakkelijk om alle bijlagen van een e-mail op te slaan met de ingebouwde functie Alle bijlagen opslaan in Outlook. Als u echter alle bijlagen van meerdere e-mails tegelijk wilt opslaan, is er geen directe functie die u kan helpen. U moet de functie Alle bijlagen opslaan in elke e-mail herhaaldelijk toepassen totdat alle bijlagen van die e-mails zijn opgeslagen. Dat kost veel tijd. In dit artikel introduceren we twee methoden waarmee u alle bijlagen van meerdere e-mails eenvoudig in Outlook in een specifieke map kunt opslaan.

Bewaar alle bijlagen van meerdere e-mails in een map met VBA-code
Meerdere klikken om alle bijlagen van meerdere e-mails in een map op te slaan met een geweldige tool


Bewaar alle bijlagen van meerdere e-mails in een map met VBA-code

In dit gedeelte wordt een VBA-code gedemonstreerd in een stapsgewijze handleiding om u te helpen snel alle bijlagen van meerdere e-mails tegelijk in een specifieke map op te slaan. Ga als volgt te werk.

1. Allereerst moet u een map aanmaken om de bijlagen op uw computer op te slaan.

Stap in de documenten map en maak een map met de naam "Bijlagen". Zie screenshot:

2. Selecteer de e-mails waarin u de bijlagen wilt opslaan, en druk vervolgens op anders + F11 toetsen om de te openen Microsoft Visual Basic voor toepassingen venster.

3. klikken Invoegen > Module om de te openen Module venster en kopieer vervolgens een van de volgende VBA-code naar het venster.

VBA-code 1: bewaar bijlagen van meerdere e-mails in bulk (bewaar bijlagen met exact dezelfde naam direct)

Tips: Deze code slaat bijlagen met exact dezelfde naam op door de cijfers 1, 2, 3 ... na bestandsnamen toe te voegen.

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
        If xSaveFiles <> "" Then
            If xMailItem.BodyFormat <> olFormatHTML Then
                xMailItem.Body = vbCrLf & "The file(s) were saved to " & xSaveFiles & vbCrLf & xMailItem.Body
            Else
                xMailItem.HTMLBody = "<p>" & "The file(s) were saved to " & xSaveFiles & "</p>" & xMailItem.HTMLBody
            End If
        End If
        xMailItem.Save
    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
VBA-code 2: bewaar bijlagen van meerdere e-mails in bulk (controleer op duplicaten)
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
        If xSaveFiles <> "" Then
            If xMailItem.BodyFormat <> olFormatHTML Then
                xMailItem.Body = vbCrLf & "The file(s) were saved to " & xSaveFiles & vbCrLf & xMailItem.Body
            Else
                xMailItem.HTMLBody = "<p>" & "The file(s) were saved to " & xSaveFiles & "</p>" & xMailItem.HTMLBody
            End If
        End If
        xMailItem.Save
    End If
Next
Set xAttachments = Nothing
Set xMailItem = Nothing
Set xSelection = Nothing
End Sub

Notes:

1) Als u alle bijlagen met dezelfde naam in een map wilt opslaan, past u het bovenstaande toe VBA-code 1. Voordat u deze code uitvoert, klikt u op Toolbox > Referentiesen controleer vervolgens de Microsoft Scripting Runtime box aan de Referenties - Project dialoog venster;

doc bijlagen opslaan 07

2) Als u wilt controleren of er dubbele bijlagen zijn, past u de VBA-code 2 toe. Ja or Nee op basis van uw behoeften.

5. druk de F5 sleutel om de code uit te voeren.

Alle bijlagen in geselecteerde e-mails worden vervolgens opgeslagen in de map die u in stap 1 hebt gemaakt. U zult zien dat er hyperlinks worden weergegeven in geselecteerde e-mails. Als u op de link klikt, wordt de specifieke bijlage automatisch geopend.

Opmerking: Er kan een Microsoft Outlook promptvenster verschijnt, klik dan op het Allow knop om door te gaan.


Bewaar alle bijlagen van meerdere e-mails in een map met een geweldige tool

Als je een newbie bent in VBA, raad je hier ten zeerste de Bewaar alle bijlagen nut van Kutools voor Outook voor jou. Met dit hulpprogramma kunt u snel alle bijlagen van meerdere e-mails tegelijk opslaan met enkele klikken, alleen in Outlook.
Voordat u de functie toepast, alstublieft download en installeer eerst Kutools voor Outlook.

1. Selecteer de e-mails met de bijlagen die u wilt opslaan.

Tips: U kunt meerdere niet-aangrenzende e-mails selecteren door de Ctrl toets en selecteer ze een voor een;
Of selecteer meerdere aangrenzende e-mails door de Shift -toets en selecteer de eerste e-mail en de laatste.

2. klikken Kutools >Hulpmiddelen voor bijlagenSave All. Zie screenshot:

3. In de Instellingen opslaan dialoogvenster, klik op de om een ​​map te selecteren waarin u de bijlagen wilt opslaan, en klik vervolgens op het OK knop.

3. klikken OK twee keer in het volgende pop-upvenster in het dialoogvenster, dan worden alle bijlagen in geselecteerde e-mails tegelijk in de opgegeven map opgeslagen.

Opmerkingen:

  • 1. Als u bijlagen in verschillende mappen wilt opslaan op basis van e-mails, controleer dan de Maak submappen in de volgende stijl box en kies een mapstijl uit de vervolgkeuzelijst.
  • 2. Naast het opslaan van alle bijlagen, kunt u bijlagen opslaan onder specifieke voorwaarden. Als u bijvoorbeeld alleen de pdf-bestandsbijlagen wilt opslaan waarvan de bestandsnaam het woord "Factuur" bevat, klikt u op de geavanceerde opties knop om de voorwaarden uit te vouwen en configureer vervolgens zoals de onderstaande afbeelding wordt weergegeven.
  • 3. Als u automatisch bijlagen wilt opslaan wanneer u een e-mail ontvangt, kunt u het Bijlagen automatisch opslaan functie kan helpen.
  • 4. Om de bijlagen rechtstreeks uit geselecteerde e-mails te verwijderen, de Maak alle bijlagen los kenmerk van Kutools for Outlook kan je een plezier doen.

  Als u een gratis proefperiode (60 dagen) van dit hulpprogramma wilt, klik om het te downloaden, en ga vervolgens de bewerking toepassen volgens de bovenstaande stappen.


Gerelateerde artikelen

Voeg bijlagen in de hoofdtekst van e-mailberichten in Outlook in
Normaal gesproken worden bijlagen weergegeven in het veld Bijgevoegd in een samenstellende e-mail. Hier biedt deze zelfstudie methoden waarmee u eenvoudig bijlagen in de e-mailtekst in Outlook kunt invoegen.

Automatisch bijlagen downloaden / opslaan vanuit Outlook naar een bepaalde map
Over het algemeen kunt u alle bijlagen van één e-mail opslaan door te klikken op Bijlagen> Alle bijlagen opslaan in Outlook. Maar als u alle bijlagen van alle ontvangen e-mails en ontvangen e-mails wilt opslaan, een ideaal dan? Dit artikel introduceert twee oplossingen om automatisch bijlagen van Outlook naar een bepaalde map te downloaden.

Druk alle bijlagen af ​​in één / meerdere e-mails in Outlook
Zoals u weet, wordt alleen de e-mailinhoud, zoals de koptekst en de hoofdtekst, afgedrukt wanneer u op Bestand> Afdrukken klikt in Microsoft Outlook, maar de bijlagen worden niet afgedrukt. Hier laten we u zien hoe u alle bijlagen in een geselecteerde e-mail op uw gemak kunt afdrukken in Microsoft Outlook.

Zoek woorden in bijlage (inhoud) in Outlook
Wanneer we een trefwoord typen in het vak Direct zoeken in Outlook, zal het trefwoord zoeken in de onderwerpen, hoofdtekst, bijlagen, enz. Van e-mails. Maar nu hoef ik alleen maar het trefwoord te zoeken in de inhoud van bijlagen alleen in Outlook, enig idee? Dit artikel toont u de gedetailleerde stappen om eenvoudig naar woorden te zoeken in de inhoud van bijlagen in Outlook.

Bewaar bijlagen bij het beantwoorden in Outlook
Wanneer we een e-mailbericht doorsturen in Microsoft Outlook, blijven de originele bijlagen in dit e-mailbericht in het doorgestuurde bericht. Als we echter een e-mailbericht beantwoorden, worden de originele bijlagen niet bij het nieuwe antwoordbericht gevoegd. Hier gaan we een paar trucjes introduceren om originele bijlagen te behouden bij het beantwoorden in Microsoft Outlook.


Kutools voor Outlook - Brengt 100 geavanceerde functies naar Outlook en maakt het werk veel gemakkelijker!

  • Auto CC / BCC volgens regels bij het verzenden van e-mail; Automatisch doorsturen Meerdere e-mails op maat; Auto antwoord zonder uitwisselingsserver, en meer automatische functies ...
  • BCC-waarschuwing - toon bericht wanneer u alle probeert te beantwoorden als uw e-mailadres in de BCC-lijst staat; Herinner bij ontbrekende bijlagen, en meer herinneren functies ...
  • Beantwoorden (alle) met alle bijlagen in het e-mailgesprek; Beantwoord veel e-mails in seconden; Begroeting automatisch toevoegen wanneer antwoord; Datum toevoegen aan onderwerp ...
  • Hulpmiddelen voor bijlagen: beheer alle bijlagen in alle e-mails, Automatisch loskoppelen, Alles comprimeren, Alles hernoemen, Alles opslaan ... Snel rapport, Tel geselecteerde e-mails...
  • Krachtige ongewenste e-mails op maat; Verwijder dubbele e-mails en contacten... Stel u in staat om slimmer, sneller en beter te doen in Outlook.
shot kutools outlook kutools tabblad 1180x121
shot kutools vooruitzichten kutools plus tabblad 1180x121
 
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Tammy · 2 months ago
    This has saved me so much time! VBA code 1 doesnt save embedded pictures, where as VBA code 2 does but doesnt rename the file with the same name. Is there a way to have VBA code 1 save the attachments and the embedded files and rename/append any that have the same file name? 
    • To post as a guest, your comment is unpublished.
      Tammy · 2 months ago
      Ok, VBA code 1, I just commented out two lines.  LINE 29 and LINE 36   -   LINE 29  LINE 36  'End If
      The VBA code 1 altered for me is as follows.  And saves embedded files and attachments and renames them with a 1,2 etc at the end! 


      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
      If xSaveFiles <> "" Then
      If xMailItem.BodyFormat <> olFormatHTML Then
      xMailItem.Body = vbCrLf & "The file(s) were saved to " & xSaveFiles & vbCrLf & xMailItem.Body
      Else
      xMailItem.HTMLBody = "<p>" & "The file(s) were saved to " & xSaveFiles & "</p>" & xMailItem.HTMLBody
      End If
      End If
      xMailItem.Save
      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
  • To post as a guest, your comment is unpublished.
    Tammy · 2 months ago
    This has saved me so much time! VBA code 1 doesnt save embedded pictures, where as VBA code 2 does but doesnt rename the file with the same name. Is there a way to have VBA code save the attachments and the embedded files? 
  • To post as a guest, your comment is unpublished.
    John · 3 months ago
    hi, if i run it, can the edited email (that shows hyperlink) be seen by the other party on the email?
    afraid of making it awkward
    • To post as a guest, your comment is unpublished.
      Tammy · 2 months ago
      I wondered this also
  • To post as a guest, your comment is unpublished.
    napiwn@gmail.com · 3 months ago
    The Vba 1 code is great. A request to add the date of the email to the file name of the attachment (yyyy-mm-dd ssmm)
  • To post as a guest, your comment is unpublished.
    Steve · 3 months ago
    Is it possible to have a variation on "VBA Code 2: Bulk Save Attachments From Multiple Emails (Check For Duplicates)" where it automatically replaces a duplicate?
  • To post as a guest, your comment is unpublished.
    Compstuff · 7 months ago
    THANK YOU so very much for this script! It has helped me do something I have wanted & needed to do for many years. Are the following modifications still valid for VBA Code 1?

    1)
    This works only if you use the Documents folder. For a more general case:
    delete line 12
    line 15 should read: strFolderpath = "C:\folder\otherfolder\"

    4)
    do not modify the emails in any way (e.g. adding the file save path to the text of the mail), delete line 26-39
  • To post as a guest, your comment is unpublished.
    Alex · 9 months ago
    Hi! I have used code 1 for many times (can't thank You enough for my saved time) and it run smooth. All of a sudden (outlook 2016), without me changing any of settings, it stopped working with message: "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." I have tried changing settings in Outlook but none of available options gave result (Trust center settings / Macros)
    • To post as a guest, your comment is unpublished.
      crystal · 9 months ago
      Hi Alex,
      It seems that macros are disabled in Outlook. Please follow the steps as the below screenshot shown to enable all macros, and then restart Outlook.

  • To post as a guest, your comment is unpublished.
    Stephen · 9 months ago
    It's okay I found the necessary tool to save attachments.
  • To post as a guest, your comment is unpublished.
    Stephen · 9 months ago
    I install Kutools and now I can't run any other macros including this one. I really need to save a whole heap of attachment, but Kutools let's you save emails but it doesn't save attachments too.
  • To post as a guest, your comment is unpublished.
    Tara · 10 months ago
    Hello,

    I am having issues with the area

    Function FileRename(xFilePath As String) As String
    Dim xPath As String
    Dim xFso As FileSystemObject

    I am getting a Complie error: User-defined tyoe not defined.
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi,
      Before running the code, please click Tools > References, and then check the Microsoft Scripting Runtime box in the References - Project dialog box.
  • To post as a guest, your comment is unpublished.
    John · 1 years ago
    Thank you, but only works if the attachments each have different names. For same name attachments, it will just overwrite the previous file. So I edited this VBA script to prefix each file saved with a number:


    Public Sub SaveAttachments()
    'Update 20191101
    Dim objOL As Outlook.Application
    Dim objMsg As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objSelection As Outlook.Selection
    Dim i As Long
    Dim lngCount As Long
    Dim strFile As String
    Dim strFolderpath As String
    Dim strDeletedFiles As String
    strFolderpath = CreateObject("WScript.Shell").SpecialFolders(16)
    Set objOL = CreateObject("Outlook.Application")
    Set objSelection = objOL.ActiveExplorer.Selection
    strFolderpath = strFolderpath & "\Attachments\"
    Dim i2 As Long
    i2 = 0
    For Each objMsg In objSelection
    i2 = i2 + 1
    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
    strDeletedFiles = ""
    If lngCount > 0 Then
    For i = lngCount To 1 Step -1
    strFile = objAttachments.Item(i).FileName
    strFile = strFolderpath & CStr(i2) & "_" & strFile
    objAttachments.Item(i).SaveAsFile strFile
    'objAttachments.Item(i).Delete()
    If objMsg.BodyFormat <> olFormatHTML Then
    strDeletedFiles = strDeletedFiles & vbCrLf & "!>
  • To post as a guest, your comment is unpublished.
    Calpa · 1 years ago
    Works great! if files have different namnes, as stated in earlier comment, I know my files will have same name, can you adjust the code anyway so that it works with same name files? Maybe adding time stamp to the file name? or just digits 1, 2, 3 etc (But not digits after filetype code, eg. ".csv 1"...) Thanks alot!!
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Calpa,
      The code has been updated. For solving your problem, please apply the VBA code 1 in the tutorial.
  • To post as a guest, your comment is unpublished.
    Carl · 1 years ago
    First code worked well, but my attachments have exact same name, can I save all of them in some way? Thanks!
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Carl,
      The code has been updated. For solving your problem, please apply the VBA code 1 in the tutorial.
  • To post as a guest, your comment is unpublished.
    Adam · 1 years ago
    Is it possible to name the files based off the subject line. You know how you can edit the subject line from received emails i organize my emails by customer name and date. Can I have the script use the subject line as the file name?
  • To post as a guest, your comment is unpublished.
    Paul · 1 years ago
    You are a golden God. Thank you!
  • To post as a guest, your comment is unpublished.
    BB · 1 years ago
    OMG thank you!!!! The VBA code worked! Just saved me HOURS of work and saved my sanity!
  • To post as a guest, your comment is unpublished.
    Alan · 1 years ago
    VBA code works great but it doesn't check for duplicate filenames - just overwrites them. Can that be added?

    ALan
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Alan,
      The code I replied to you before has some problems. I have added new codes to the tutorial, and the problem you mentioned have been solved. Please have a look and try it. Thank you!
  • To post as a guest, your comment is unpublished.
    Mike · 1 years ago
    The VBA code worked GREAT! Thanks.
  • To post as a guest, your comment is unpublished.
    outlookuser · 1 years ago
    Thank you! really help me a lot!!
  • To post as a guest, your comment is unpublished.
    Priyanka · 1 years ago
    I have applied this VBA to few mails. How to undo this? I dont want those messages in all mails (The file(s) were saved to .....). Please help.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Priyanka,
      The VBA code does not support Undo operation. Sorry for the inconvenience.
  • To post as a guest, your comment is unpublished.
    Hiep · 2 years ago
    Thank you. It saves me lots of time.
  • To post as a guest, your comment is unpublished.
    BG Davis · 2 years ago
    So I click the link "Kutools for outlook" and I'm directed to a page that is not that, but "Kutools - Combines More Than 300 Advanced Functions and Tools for Microsoft Excel."

    Nothing about Outlook. Waste of time.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Davis,
      We created an incorrect hyperlink. Thanks for your reminder! And sorry for the inconvenience brought to you.
  • To post as a guest, your comment is unpublished.
    Brian · 4 years ago
    This is what the code is at now, and it does save all the attachments, but it only adds text to the first message. Can anyone help me with this?

    Public Sub SaveAttachments()
    'Update 20170523
    Dim objOL As Outlook.Application
    Dim objMsg As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objSelection As Outlook.Selection
    Dim I As Long
    Dim lngCount As Long
    Dim strFile As String
    Dim strFolderpath As String
    Set objOL = CreateObject("Outlook.Application")
    Set objSelection = objOL.ActiveExplorer.Selection
    strFolderpath = "C:\Users\brianp\Documents\Attachments\"
    For Each objMsg In objSelection
    Set objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
    'Use this to test MsgBox "Subject = " & objMsg.Subject & " lngCount = " & objAttachments.Count
    If lngCount > 0 Then
    For I = lngCount To 1 Step -1
    strFile = objAttachments.Item(I).FileName
    strFile = strFolderpath & strFile
    objAttachments.Item(I).SaveAsFile strFile
    Next I
    End If
    If objMsg.BodyFormat olFormatHTML Then
    objMsg.Body = vbCrLf & "The Attached file(s) were saved to " & "" & strFile & "" & vbCrLf & objMsg.Body
    Else
    objMsg.HTMLBody = "" & "The Attached file(s) were saved to " & "" & strFile & "" & "" & objMsg.HTMLBody
    End If
    Next
    ExitSub:
    Set objAttachments = Nothing
    Set objMsg = Nothing
    Set objSelection = Nothing
    Set objOL = Nothing
    End Sub
  • To post as a guest, your comment is unpublished.
    Brian · 4 years ago
    I can get this to run but how and the objSelection.Count is 2 but it will only save the attachments on the first email.
  • To post as a guest, your comment is unpublished.
    Atron Seige · 4 years ago
    Thanks! This saved me a lot of time and frustration!
  • To post as a guest, your comment is unpublished.
    Josh Fernandez · 4 years ago
    Works great with no problems! Thanks. Saved me a bunch of time!

    Thanks,

    Josh
  • To post as a guest, your comment is unpublished.
    Sathish · 5 years ago
    How to remove the "The file(s) were saved to" which is showing below.....
    • To post as a guest, your comment is unpublished.
      mallary.webb · 7 months ago
      I just adjusted the code after "Next i" and it worked fine:
      Next i
      If xSaveFiles <> "" Then
      If xMailItem.BodyFormat <> olFormatHTML Then

      Else

      End If
  • To post as a guest, your comment is unpublished.
    debsdebsdebs · 6 years ago
    This worked great except for one problem. The attachments in my emails are all named the same thing, so when they copy over, the script keeps replacing the same file with the next attachment in the queue. Is there any way to make it rename them rather than rewrite them?

    Thanks!
  • To post as a guest, your comment is unpublished.
    tweazee · 7 years ago
    To sharon -- The below website fixes your issue.

    www_dot_outlook-tips_dot_net/code-samples/save-and-delete-attachments/

    It does NOT have the timestamp feature code that TXgardner provided above, so if you want that, you have to edit your code.
  • To post as a guest, your comment is unpublished.
    sharon · 7 years ago
    Hi..I tried everything on here but I keep getting Complile Error block if without end if. I made adjustments per Thomas' suggestions. Heres the code..what am I missing? Any help is appreciated.
    Public Sub SaveAttachments()
    'Update 20130828
    Dim objOL As Outlook.Application
    Dim objMsg As Outlook.MailItem
    Dim objAttachments As Outlook.Attachments
    Dim objSelection As Outlook.Selection
    Dim i As Long
    Dim lngCount As Long
    Dim strFile As String
    Dim strFolderpath As String
    Dim strDeletedFiles As String
    objOL = CreateObject("Outlook.Application")
    objSelection = objOL.ActiveExplorer.Selection
    strFolderpath = "C:\folder\Attachments\"
    For Each objMsg In objSelection
    objAttachments = objMsg.Attachments
    lngCount = objAttachments.Count
    strDeletedFiles = ""
    If lngCount > 0 Then
    For i = lngCount To 1 Step -1
    strFile = objAttachments.Item(i).FileName
    strFile = strFolderpath & strFile
    objAttachments.Item(i).SaveAsFile strFile
    DoEvents
    'objAttachments.Item(i).Delete()
    Next
    ExitSub:
    objAttachments = Nothing
    objMsg = Nothing
    Set objSelection = objOL.ActiveExplorer.Selection
    objOL = Nothing
    End Sub
  • To post as a guest, your comment is unpublished.
    TXgardener · 7 years ago
    Following on the suggestions above, I had daily system generated emails with attached 'report.txt' and needed to append the sent date to the saved file name in order to avoid overwrites and to distinguish files. Made the following adds in the appropriate places:
    add- Dim strSent As String
    add- strSent = Format(objMsg.SentOn, "yymmdd")
    add- strFile = strSent & strFile

    Saved files are now 140822Report.txt, etc.
  • To post as a guest, your comment is unpublished.
    Thomas · 7 years ago
    Thanks for sharing.

    I agree with Stephan on his first two points, see some clarification on his point 3. Finally, adding a DoEvents at the right place should allow you to process large number of emails (just run this code on 157 mails in Outlook 2013).

    Some additional thoughts:

    1)
    This works only if you use the Documents folder. For a more general case:
    delete line 12
    line 15 should read: strFolderpath = "C:\folder\otherfolder\"

    substitute [i]C:\folder\otherfolder\[/i] with whatever path you have.

    2)
    This code will not delete the attachments, if you want that just delete the leading ' from line 25.

    3)
    If you have big attachments, then probably it is a good idea (as Stephan noted) to put a DoEvents function after line 24.

    4)
    I personally do not want to modify the emails in any way (e.g. adding the file save path to the text of the mail), if you agree with me then you can delete line 26-39.

    5)
    If you skip my step 4, then you can allow the program to modify the emails by checking "Allow access for x time", then you have to click allow only once (c.f. step 6 above in the original).
  • To post as a guest, your comment is unpublished.
    Stephan · 7 years ago
    Some corrections:

    1. objMsg.Save 'without ()
    2. Objects must be assigned with SET (e. g. SET objSelection = objOL.ActiveExplorer.Selection)

    3. The main Loop should contain a DoEvents in order to prevent blankout by Outlook.

    4. When processing a lot of mails (more than 100), Outlook may Crash. It seems that there is a Memory leak somewhere.

    Just my 2 Cents.

    Apart from the bugs (probably due to Outlook 2013) this is really nice and working. Thanks a lot for sharing.
  • To post as a guest, your comment is unpublished.
    Alain · 7 years ago
    Same problem syntax error on objMsg.Save() ...
  • To post as a guest, your comment is unpublished.
    Valencia · 7 years ago
    When I try to run this I get syntax error on objMsg.Save() - expects =
  • To post as a guest, your comment is unpublished.
    Chris · 7 years ago
    When I try to run this I get syntax error on objMsg.Save() - expects =