The following VBA script shows how to remove attachments from emails in Outlook and save them to your local machine.
The script will also note in the email body that the attachment has been removed in the email body.
This can help reducing the size of your mailbox, or removing potentially confidential or sensitive attachments.
- Microsoft Outlook installed on your computer.
- Familiarity with VBA (Visual Basic for Applications).
- Open Microsoft Outlook
- Open the VBA Editor by Pressing
Alt + F11
- Right-click on ‘Project1’ (or similar), and select Insert -> Module
- Copy the VBA script below and paste it into the module window.
Sub RemoveAttachments() Dim objItem As Object Dim objAtt As Outlook.Attachment Dim strFolder As String Dim strFile As String Dim strMsg As String Dim strSep As String
' Change the folder path as per your requirement strFolder = "C:\Temp\" For Each objItem In Application.ActiveExplorer.Selection If objItem.Attachments.Count > 0 Then strMsg = "========================================" & vbCrLf strMsg = strMsg & "Attachments removed on " & Format(Now, "yyyy-mm-dd hh:mm:ss") & vbCrLf strMsg = strMsg & "========================================" & vbCrLf strMsg = strMsg & "The following attachment(s) were removed:" & vbCrLf For Each objAtt In objItem.Attachments ' Use the email's date/time stamp as the unique identifier strFile = Format(objItem.ReceivedTime, "yyyy-mm-dd_hhmmss_") & objAtt.FileName objAtt.SaveAsFile strFolder & strFile strMsg = strMsg & vbCrLf & objAtt.FileName objAtt.Delete Next objAtt strMsg = strMsg & vbCrLf & vbCrLf strSep = String(40, "=") objItem.Body = objItem.Body & vbCrLf & vbCrLf & strSep & vbCrLf & strMsg & strSep objItem.Save End If Next objItem
- Run the Script: Press
F5or click on the
Runbutton to execute the script.
- Select the emails you wish to process, the script will loop through each selected email, remove the attachments, save them to the specified folder (
C:\Temp\), and append a message in the email body documenting the action.
How the Script Works:
- Variable Declaration: Variables are declared to hold objects and string values used in the script.
- Folder Path Setting:
strFolderis set to “C:\Temp\”, change this to your desired directory.
- Outer Loop (Email Selection): Iterates through each selected email item in Outlook.
- Attachment Check: Checks if the current email item has any attachments.
- Inner Loop (Attachment Processing): Iterates through each attachment, saves it to the specified folder, and then removes it from the email.
- Message Construction and Email Update: Constructs a message, appends it to the email body, and saves the email item.