Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Outlook Macro to Delete Emails from a folder

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
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…
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…

670 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