Gå til hovedindhold

Hvordan gemmes alle vedhæftede filer fra flere e-mails til mappen i Outlook?

Det er nemt at gemme alle vedhæftede filer fra en e-mail med den indbyggede funktion Gem alle vedhæftede filer i Outlook. Men hvis du vil gemme alle vedhæftede filer fra flere e-mails på én gang, er der ingen direkte funktion, der kan hjælpe. Du skal gentagne gange anvende funktionen Gem alle vedhæftede filer i hver e-mail, indtil alle vedhæftede filer gemmes fra disse e-mails. Det er tidskrævende. I denne artikel introducerer vi to metoder, så du nemt kan gemme alle vedhæftede filer fra flere e-mails til en bestemt mappe i Outlook.

Gem alle vedhæftede filer fra flere e-mails til mappen med VBA-kode
Flere klik for at gemme alle vedhæftede filer fra flere e-mails til mappen med et fantastisk værktøj


Gem alle vedhæftede filer fra flere e-mails til mappen med VBA-kode

Dette afsnit demonstrerer en VBA-kode i en trinvis vejledning, der hjælper dig med hurtigt at gemme alle vedhæftede filer fra flere e-mails til en bestemt mappe på én gang. Gør som følger.

1. For det første skal du oprette en mappe til at gemme vedhæftede filer på din computer.

Kom ind i Dokumenter mappe og opret en mappe med navnet “Vedhæftede filer”. Se skærmbillede:

2. Vælg de e-mails, som de vedhæftede filer, du vil gemme, og tryk derefter på andre + F11 nøgler til at åbne Microsoft Visual Basic til applikationer vindue.

3. klik indsatte > Moduler at åbne Moduler vindue, og kopier derefter en af ​​følgende VBA-koder i vinduet.

VBA-kode 1: Bulk gem vedhæftede filer fra flere e-mails (gem vedhæftede filer med samme navn direkte)

Tips: Denne kode gemmer nøjagtigt samme vedhæftede navn ved at tilføje cifre 1, 2, 3 ... efter filnavne.

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
VBA-kode 2: Gem vedhæftede filer fra flere e-mails (tjek for dubletter)
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

Noter:

1) Hvis du vil gemme alle vedhæftede filer i samme mappe i en mappe, skal du anvende ovenstående VBA-kode 1. Inden du kører denne kode, skal du klikke Værktøjer > Referencer, og tjek derefter Microsoft Scripting Runtime boks i Referencer - Projekt dialog boks;

doc gem vedhæftede filer 07

2) Hvis du vil kontrollere, om der er duplikatnavne til vedhæftede filer, skal du anvende VBA-koden 2. Når koden er kørt, vises en dialogboks, der minder dig om, hvorvidt du skal udskifte de vedhæftede filer. Ja or Ingen baseret på dine behov.

5. Tryk på F5 nøgle for at køre koden.

Derefter gemmes alle vedhæftede filer i valgte e-mails i den mappe, du oprettede i trin 1. 

Bemærkninger: Der kan være en Microsoft Outlook hurtig boks dukker op, skal du klikke på Tillad knappen for at gå videre.


Gem alle vedhæftede filer fra flere e-mails til mappen med et fantastisk værktøj

Hvis du er nybegynder i VBA, anbefales det stærkt Gem alle vedhæftede filer nytte af Kutools til Outook for dig. Med dette værktøj kan du hurtigt gemme alle vedhæftede filer fra flere e-mails på én gang med flere klik kun i Outlook.
Inden du anvender funktionen, bedes du venligst download og installer Kutools til Outlook først.

1. Vælg de e-mails, der indeholder de vedhæftede filer, du vil gemme.

tips: Du kan vælge flere ikke-tilstødende e-mails ved at holde Ctrl tast og vælg dem en efter en;
Eller vælg flere tilstødende e-mails ved at holde nede Flytte tasten, og vælg den første e-mail og den sidste.

2. klik Kutools >VedhæftningsværktøjerGem alle. Se skærmbillede:

3. i Gem indstillinger dialog, klik på for at vælge en mappe, der skal gemmes vedhæftede filer, og klik derefter på OK .

3. klik OK to gange i den næste dukker op til dialogboksen, så gemmes alle vedhæftede filer i valgte e-mails i den angivne mappe på én gang.

Bemærkninger:

  • 1. Hvis du vil gemme vedhæftede filer i forskellige mapper baseret på e-mails, skal du kontrollere Opret undermapper i følgende stil og vælg en mappestil fra rullemenuen.
  • 2. Udover at gemme alle vedhæftede filer, kan du gemme vedhæftede filer under bestemte betingelser. For eksempel vil du kun gemme pdf-vedhæftede filer, som filnavnet indeholder ordet "Faktura", klik venligst på Avancerede indstillinger -knappen for at udvide betingelserne og derefter konfigurere som nedenstående skærmbillede vist.
  • 3. Hvis du automatisk vil gemme vedhæftede filer, når du modtager e-mail, vises Gem vedhæftede filer automatisk funktion kan hjælpe.
  • 4. For at fjerne vedhæftede filer direkte fra valgte e-mails, Fjern alle vedhæftede filer træk ved Kutools til Outlook kan gøre dig en tjeneste.

  Hvis du vil have en gratis prøveperiode (60 dage) af dette værktøj, klik for at downloade det, og gå derefter til at anvende handlingen i henhold til ovenstående trin.


Relaterede artikler

Indsæt vedhæftede filer i selve e-mail-beskeden i Outlook
Normalt vises vedhæftede filer i feltet Vedhæftet i en komponerende e-mail. Her giver denne vejledning metoder til at hjælpe dig med nemt at indsætte vedhæftede filer i e-mail-kroppen i Outlook.

Download / gem automatisk vedhæftede filer fra Outlook til en bestemt mappe
Generelt kan du gemme alle vedhæftede filer i en e-mail ved at klikke på Vedhæftede filer> Gem alle vedhæftede filer i Outlook. Men hvis du har brug for at gemme alle vedhæftede filer fra alle modtagne e-mails og modtage e-mails, noget ideelt? Denne artikel introducerer to løsninger til automatisk download af vedhæftede filer fra Outlook til en bestemt mappe.

Udskriv alle vedhæftede filer i en / flere e-mails i Outlook
Som du ved, udskriver det kun e-mail-indholdet som f.eks. Overskrift, brødtekst, når du klikker på Filer> Udskriv i Microsoft Outlook, men ikke udskriver vedhæftede filer. Her viser vi dig, hvordan du nemt udskriver alle vedhæftede filer i en valgt e-mail i Microsoft Outlook.

Søg efter ord inden for vedhæftet fil (indhold) i Outlook
Når vi skriver et nøgleord i feltet Øjeblikkelig søgning i Outlook, søger det nøgleordet i mails, emner, organer, vedhæftede filer osv. Men nu skal jeg bare søge på nøgleordet i vedhæftet filindhold kun i Outlook, nogen idé? Denne artikel viser dig de detaljerede trin til let at søge på ord i vedhæftet filindhold i Outlook.

Behold vedhæftede filer, når du svarer i Outlook
Når vi videresender en e-mail-meddelelse i Microsoft Outlook, forbliver originale vedhæftede filer i denne e-mail-meddelelse i den videresendte besked. Når vi svarer på en e-mail-besked, vedhæftes de originale vedhæftede filer imidlertid ikke i den nye svarmeddelelse. Her vil vi introducere et par tricks til at beholde originale vedhæftede filer, når du svarer i Microsoft Outlook.


Bedste kontorproduktivitetsværktøjer

Kutools til Outlook - Over 100 kraftfulde funktioner til at superlade din Outlook

🤖 AI Mail Assistant: Øjeblikkelige pro-e-mails med AI-magi – et klik for geniale svar, perfekt tone, flersproget beherskelse. Forvandl e-mailing ubesværet! ...

📧 Email Automation: Ikke til stede (tilgængelig til POP og IMAP)  /  Planlæg Send e-mails  /  Auto CC/BCC efter regler ved afsendelse af e-mail  /  Automatisk videresendelse (avancerede regler)   /  Tilføj automatisk hilsen   /  Opdel automatisk e-mails med flere modtagere i individuelle meddelelser ...

📨 Email Management: Genkald nemt e-mails  /  Bloker svindel-e-mails af emner og andre  /  Slet duplikerede e-mails  /  Avanceret søgning  /  Konsolider mapper ...

📁 Vedhæftede filer ProBatch Gem  /  Batch adskilles  /  Batch komprimere  /  Automatisk gem   /  Automatisk afmontering  /  Automatisk komprimering ...

🌟 Interface Magic: 😊 Flere smukke og seje emojis   /  Boost din Outlook-produktivitet med fanebaserede visninger  /  Minimer Outlook i stedet for at lukke ...

👍 Wonders med et enkelt klik: Besvar alle med indgående vedhæftede filer  /   Anti-phishing e-mails  /  🕘Vis afsenderens tidszone ...

👩🏼‍🤝‍👩🏻 Kontakter og kalender: Batch Tilføj kontakter fra udvalgte e-mails  /  Opdel en kontaktgruppe til individuelle grupper  /  Fjern fødselsdagspåmindelser ...

Over 100 Features Afvent din udforskning! Klik her for at finde mere.

Læs mere       Gratis download      Køb
 

 

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