Results 1 to 2 of 2

Thread: [RESOLVED] OUTLOOK - script to keep only the most recent email in a folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    11

    Resolved [RESOLVED] OUTLOOK - script to keep only the most recent email in a folder

    Hi All,

    Everyday I receive an email with an access code in it, which is only valid for the current day.
    I have an outlook rule set up to put it in to it's own folder (on the same folder level as my Inbox).

    What I would like to do is add a script to the rule, to delete all the older emails when the new one comes in.
    I have found the code below on another forum somewhere (can't recall) which seems to work, except that you have to pick which folder you are clearing out each time.
    I would like to specify the folder, but as yet I have been unsuccessful.
    What should I replace the PickFolder line with?

    Code:
    Sub DeleteOlderThan1day()
     
    Dim oFolder As Folder
    Dim Date1day As Date
    Dim ItemsOverMonths As Outlook.Items
    Dim DateToCheck As String
     
    Date1day = DateAdd("d", -1, Now())
    Date1day = Format(Date1day, "mm/dd/yyyy")
     
    Set oFolder = Application.Session.PickFolder 'or set your folder
     
    DateToCheck = "[Received] <= """ & Date1day & """"
     
    Set ItemsOverMonths = oFolder.Items.Restrict(DateToCheck)
     
    For i = ItemsOverMonths.Count To 1 Step -1
        ItemsOverMonths.Item(i).Delete
    Next
      
    Set ItemsOverMonths = Nothing
    Set oFolder = Nothing
     
    End Sub
    Could anyone help me with this?
    (Outlook 2010, POP3 email account.)
    Thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: OUTLOOK - script to keep only the most recent email in a folder

    something like
    Code:
    Set oFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    Set oFolder = oFolder.Folders("MSG Attachments")
    this assumes that the folder is a sub folder of inbox, change folder name to suit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width