Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Outlook Macro to Delete Emails from a folder

Posted on 2010-08-19
2
Medium Priority
?
933 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
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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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.
Take a look at these 6 Outlook Email management tools which can augment the working and performance of Microsoft Outlook to give you a more rewarding emailing experience.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

886 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