You are on page 1of 4

Remove all attachments from multiple email messages in Outlook with VBA

code

If you don't prefer the VBA method, please try the third method to remove all
attachments with just several clicks.

If you want to remove all attachments from multiple email messages in Microsoft Outlook,
the following method will help you do it easily. We recommend you enable all macros in
your Microsoft Outlook firstly.

Step 1: Go to the folder of My Document, create a new folder, and name it


as OLAttachments

Step 2: Select multiple email messages whose attachments you will remove later.

Note: You can select inconsecutive email messages with holding the Ctrl key and
clicking.

You can select consecutive email messages with holding the Shift key and clicking.

Step 3: Open the VBA Editor with pressing the Alt key and F11 key at the same time.

Step 4: Expand the Project1 > Microsoft Outlook Objects in the left bar, and then
double click the ThisOutlookSession to open it in the Editor. See following screen shot:
Step 5: Copy and paste the following VBA code in the editing pane.
1 Public Sub ReplaceAttachmentsToLink()
2 Dim objApp As Outlook.Application
Dim aMail As Outlook.MailItem 'Object
3 Dim oAttachments As Outlook.Attachments
4 Dim oSelection As Outlook.Selection
5 Dim i As Long
6 Dim iCount As Long
7 Dim sFile As String
Dim sFolderPath As String
8 Dim sDeletedFiles As String
9
10 ' Get the path to your My Documents folder
11 sFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
12 On Error Resume Next
13
14 ' Instantiate an Outlook Application object.
Set objApp = CreateObject("Outlook.Application")
15
16 ' Get the collection of selected objects.
17 Set oSelection = objApp.ActiveExplorer.Selection
18
19 ' Set the Attachment folder.
20 sFolderPath = sFolderPath & "\OLAttachments"
21
22
23 ' Check each selected item for attachments. If attachments exist,
' save them to the Temp folder and strip them from the item.
24 For Each aMail In oSelection
25
26 ' This code only strips attachments from mail items.
27 ' If aMail.class=olMail Then
28 ' Get the Attachments collection of the item.
29 Set oAttachments = aMail.Attachments
iCount = oAttachments.Count
30
31
32 If iCount > 0 Then
33
34 ' We need to use a count down loop for removing items
35 ' from a collection. Otherwise, the loop counter gets
36 ' confused and only every other item is removed.
37
38 For i = iCount To 1 Step -1
39
' Save attachment before deleting from item.
40 ' Get the file name.
41 sFile = oAttachments.Item(i).FileName
42
43 ' Combine with the path to the Temp folder.
44 sFile = sFolderPath & "\" & sFile
45
46 ' Save the attachment as a file.
47 oAttachments.Item(i).SaveAsFile sFile
48
' Delete the attachment.
49 oAttachments.Item(i).Delete
50
51 'write the save as path to a string to add to the message
52 'check for html and use html tags in link
53 If aMail.BodyFormat <> olFormatHTML Then
54 sDeletedFiles = sDeletedFiles & vbCrLf & "<file://" & sFile & ">"
Else
55 sDeletedFiles = sDeletedFiles & "<br>" & "<a href='file://" & _
56 sFile & "'>" & sFile & "</a>"
57 End If
58
59
60 Next i
'End If
61
62
' Adds the filename string to the message body and save it
63 ' Check for HTML body
64 If aMail.BodyFormat <> olFormatHTML Then
65 aMail.Body = aMail.Body & vbCrLf & _
66 "The file(s) were saved to " & sDeletedFiles
Else
67 aMail.HTMLBody = aMail.HTMLBody & "<p>" & _
68 "The file(s) were saved to " & sDeletedFiles & "</p>"
69 End If
70
71 aMail.Save
72 'sets the attachment path to nothing before it moves on to the next message.
sDeletedFiles = ""
73
74 End If
75 Next 'end aMail
76
77 ExitSub:
78
79 Set oAttachments = Nothing
80 Set aMail = Nothing
Set oSelection = Nothing
81 Set objApp = Nothing
82 End Sub
83
84
85
86
87
88
89
90
91
92
93
94

Step 6: Press the key of F5 to run this VBA code.

Now all attachments from selected email messages are removed, with leaving hyperlinks
to each deleted attachment at the bottom of all selected email messages.

You might also like