Solved

Outlook Macro to Delete Emails from a folder

Posted on 2010-08-19
2
926 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

742 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