Solved

Outlook Macro to Delete Emails from a folder

Posted on 2010-08-19
2
921 Views
Last Modified: 2012-05-10
We have set up an email box that receives emails that are inturn processed by a .net application

There is a rule that moves the emails from the inbox to a folder named shops

Would like to write a macro that will delete emails in this folder that is older than 30 days but not sure how to do this.

Can anyone please give an example?

0
Comment
Question by:johnnyg123
2 Comments
 
LVL 11

Expert Comment

by:Coast-IT
Comment Utility
You can do this in Exchange via a recipient policy.

Open system manager > Recipients > Recipient Policies

Right click on Recipient policies and select new recipient policy

Select mailbox manager settings

Name the policy and modify it accordingly under the policy tab.

Click apply and that should be it

------

You can also use a GPO ;

Install ADM template for outlook.

In the GPO, go to User Configuration>Microsoft Office Outlook 2003>Other>AutoArchive.

There are "autoArchive" settings and "retention" Settings.

Options in Retention Settings are:

Maximum number of days to retain items in:
 
Inbox
All mail folders excluding Inbox
Calendar items in any folder




There is no "macro" to do this I believe, but these will work perfectly
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
Comment Utility
Hi, johnnyg123.

The macro below should take care of this.  Follow these isntructions to use it.

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
10. Run the macro named DeleteIfOverThirtyDaysOld
Sub DeleteIfOverThirtyDaysOld()

    Dim olkFld As Object, _

        olkLst As Object, _

        olkItm As Object, _

        intIdx As Integer

    'One the enxt line edit the path to the Shops folder'

    Set olkFld = OpenOutlookFolder("Path_To_The_Folder")

    Set olkLst = olkFld.Items.Restrict("[ReceivedTime] <= '" & Format(DateAdd("d", -30, Now), "ddddd h:nn AMPM") & "'")

    For intIdx = olkLst.Count To 1 Step -1

        Set olkItm = olkLst.Item(intIdx)

        If olkItm.Class = olMail Then

            olkItm.Delete

        End If

    Next

    Set olkItm = Nothing

    Set olkLst = Nothing

    Set olkFld = Nothing

End Sub



Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder

    ' Purpose: Opens an Outlook folder from a folder path.'

    ' Written: 4/24/2009'

    ' Author:  BlueDevilFan'

    ' Outlook: All versions'

    Dim arrFolders As Variant, _

        varFolder As Variant, _

        bolBeyondRoot As Boolean

    On Error Resume Next

    If strFolderPath = "" Then

        Set OpenOutlookFolder = Nothing

    Else

        Do While Left(strFolderPath, 1) = "\"

            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)

        Loop

        arrFolders = Split(strFolderPath, "\")

        For Each varFolder In arrFolders

            Select Case bolBeyondRoot

                Case False

                    Set OpenOutlookFolder = Outlook.Session.Folders(varFolder)

                    bolBeyondRoot = True

                Case True

                    Set OpenOutlookFolder = OpenOutlookFolder.Folders(varFolder)

            End Select

            If Err.Number <> 0 Then

                Set OpenOutlookFolder = Nothing

                Exit For

            End If

        Next

    End If

    On Error GoTo 0

End Function

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Resolve DNS query failed errors for Exchange
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now