Solved

Outlook 2007 Sub Folder Item deletion

Posted on 2012-03-28
3
827 Views
Last Modified: 2012-08-13
I have some code that works well for removing items from top level folders but I cannot figure out how to search down into a sub-folder to remove items.
Can someone please help with an update/append to the code ?

Folders: Root / Hyperion / Essbase Loads

Public Sub RemoveEssbaseUpdates()
     'Deletes messages from example@mail.com that have "Test Mail"
     'in the subject line and are older than the newest message.
    Dim olSession As Outlook.Application, olNamespace As NameSpace
    Dim olInbox As Outlook.MAPIFolder
    Dim olDeleted As Outlook.MAPIFolder
    Dim olSent As Outlook.MAPIFolder
    Dim olFolder As MAPIFolder
    Dim oldeletedFolder As MAPIFolder
    Dim olSentFolder As MAPIFolder
    Dim olItems As Items
    Dim olItems2 As Items
    Dim olItems3 As Items
    Dim i As Integer
 
    Set olSession = New Outlook.Application
    Set olNamespace = olSession.GetNamespace("MAPI")
    Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox)
    Set olDeleted = olNamespace.GetDefaultFolder(olFolderDeletedItems)
    Set olSent = olNamespace.GetDefaultFolder(olFolderSentMail)
    Set olFolder = olInbox.Folders("Essbase Loads")
    'Set oldeletedFolder = olDeleted.Folders("Deleted Items")
    Set olItems = olFolder.Items
    Set olItems2 = olDeleted.Items
    Set olItems3 = olSent.Items
 
   
    If olItems.Count <= 1 Then GoTo Release                 ' don't want to delete anything if there's a single message or no messages in folder
    olItems.Sort ("ReceivedTime")                           ' sort all current items by received time in descending order
    olItems.GetLast
    For i = olItems.Count To 1 Step -1
        If TypeName(olItems.Item(i)) = "MailItem" Then
           
                    olItems.Item(i).Delete
                'End If
            'End If
        End If
    Next
   
   
   
   
Release:
    Set olSession = Nothing
    Set olNamespace = Nothing
    Set olInbox = Nothing
    Set olDeleted = Nothing
    Set olSent = Nothing
    Set olFolder = Nothing
    Set oldeletedFolder = Nothing
    Set olSentFolder = Nothing
    Set olItems = Nothing
    Set olItems2 = Nothing
    Set olItems3 = Nothing
End Sub
0
Comment
Question by:cfranz70
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 37780999
If I understand correctly then:

Root / Hyperion / Essbase Loads

Becomes for example:

olNamespace.GetDefaultFolder(olFolderInbox).folders("Hyperion").folders("Essbase Loads")

Chris
0
 

Author Comment

by:cfranz70
ID: 37783389
Thanks Chris, I will try that.  I think you might be right on first glance.  Will circle back asap
0
 

Author Closing Comment

by:cfranz70
ID: 37783642
Thanks Chris.  Silly I did not see that but I appreciate your help.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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.
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
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…

778 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