Solved

Outlook 2007 Sub Folder Item deletion

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Outlook Free & Paid Tools
In this step by step procedure, you will come to know the details of creating an Outlook meeting in 2007, 2010, 2013 & 2016.
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…
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…

860 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