Solved

Outlook 2007 Sub Folder Item deletion

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Changing a few Outlook Options can help keep you organized!
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

630 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