Solved

Outlook 2007 Sub Folder Item deletion

Posted on 2012-03-28
3
825 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now