VBA script or macro to remove attachments in Outlook

I'm new to VBA and macros is Office so I will need step by step help with a VBA script or a macro to remove attachments from a large amount of emails in Outlook and preferably save them to a folder specified in the script. Also I would want to specify the date ranges and all folders e.g. inbox, sent items, deleted

Any ideas?
LVL 1
GerhardpetAsked:
Who is Participating?
 
David LeeConnect With a Mentor Commented:
Hi, Gerhardpet.

This should do it.  Follow these instctions to add the code to Outlook.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor

To use this

1.  Select a folder
2.  Run the macro
3.  Enter the starting and ending dates
Sub RemoveAttachments()
    Const MACRO_NAME = "Remove Attachments"
    Dim strStart As String, _
        strEnd As String, _
        strFolder As String, _
        olkFolder As Outlook.MAPIFolder, _
        olkItem As Object, _
        intCount As Integer, _
        intMsgsInDateRange As Integer, _
        intItemsWithAttachments As Integer, _
        intAttachmentsRemoved As Integer
    strStart = Format(InputBox("Enter the start date (DD/MM/YYYY:)"), "ddddd")
    strEnd = Format(InputBox("Enter the end date (DD/MM/YYYY:)"), "ddddd")
    On Error Resume Next
    If (Not IsDate(strStart)) Or (Not IsDate(strEnd)) Then
        MsgBox "No date entered.  Run again and enter a date.", vbCritical + vbOKOnly, MACRO_NAME & " - Error"
    Else
        'Change the folder path on the next line.  This is the folder that attachments will be saved to.'
        strFolder = "H:\Attachments\"
        Set olkFolder = Application.ActiveExplorer.CurrentFolder
        For Each olkItem In olkFolder.Items
            With olkItem
                'Change the dates on the next line as desired
                If (.ReceivedTime >= strStart) And (.ReceivedTime <= strEnd) Then
                    intMsgsInDateRange = intMsgsInDateRange + 1
                    If .Attachments.Count > 0 Then
                        intItemsWithAttachments = intItemsWithAttachments + 1
                        For intCount = .Attachments.Count To 1 Step -1
                            .Attachments.Item(intCount).SaveAsFile strFolder & _
                            .Attachments.Item(intCount).DisplayName
                            .Attachments.Item(intCount).Delete
                            intAttachmentsRemoved = intAttachmentsRemoved + 1
                        Next
                        .Save
                    End If
                End If
            End With
        Next
        If intMsgsInDateRange = 0 Then
            MsgBox "All done.  There were no messages in the date range specified.", vbInformation + vbOKOnly, MACRO_NAME
        Else
            MsgBox "All done!" & vbCrLf & "We removed " & intAttachmentsRemoved & " attachments from " & _
                intItemsWithAttachments & " items.", vbInformation + vbOKOnly, MACRO_NAME
        End If
    End If
    On Error GoTo 0
    Set olkItem = Nothing
    Set olkFolder = Nothing
End Sub

Open in new window

0
 
GerhardpetAuthor Commented:
This works great!

Here are a few things for the wish list. I would like to create a shortcut for the user to run this script on their own
1- Have a progress bar
2- An option so that the user can browse for the folder were to save the attachments
3- The date selectors have them with a mini calendar to select the dates from

If you have time for the above I would appreciate it

Regardless I will award you the points and thank for your help on this BlueDevilFan!
0
 
David LeeCommented:
The shortcut is easy.  You can add a toolbar button that runs the script by following these instructions.

1.  Click View > Toolbars > Customize
2.  Click the Toolbars tab
3.  Click New
4.  Name the toolbar
5.  Click the Commands tab
6.  Under Categories click Macros
7.  Under Commands click and hold on the macro, then drag it out and drop it on the new toolbar
8.  Dock the toolbar somewhere onscreen

For the other three, I'm Sorry but you're talking about more of a full-fledged application.  I'm not willing to undertake that.
0
 
GerhardpetAuthor Commented:
Ok. Thank you for you help anyway. I will just do that for the user then. I didn't know how much work that would be.
0
 
David LeeCommented:
I understand.  For whatever it's worth, the other three involve developing two forms, one to select the folder and set the dates, and another to show the progress.  Developing forms is always a cause for concern because they require installation (additional instructions and potential problems) and there's always the possibility of a mismatch between the form controls I have available to me and those available on the user's computer.  If we don't have the same controls, then the forms won't work and that will lead to additional time trying to isolate which controls are the problem and developing a workaround.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.