Solved

Outlook Macro to Delete Emails from a folder

Posted on 2010-08-19
2
927 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Expert Comment

by:Coast-IT
ID: 33476349
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
ID: 33476782
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
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: …

729 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