Avatar of Physimed
Physimed
Flag for Canada asked on

Outlook VBA Code - Delete Items in toggle folder and subfolder

I found that script in Expert Exchange

https://www.experts-exchange.com/questions/27730495/Outlook-VBA-Code-Delete-Items-in-Sub-Folder-in-additional-mailbox-when-Outlook-Opens-Code-provided-so-far.html

It's a great script

I'm looking someting like that but with one step further

is it possible to delete every item in folder and its subfolder

ex:
\\pstname\Inbox\Fred\[ALL]subFolder

assuming that there are a lot of subs and they can possibly change

so I want the script able to look all sub folders in a dynamical way
OutlookVBA

Avatar of undefined
Last Comment
Physimed

8/22/2022 - Mon
Qlemo

That's a bit more complex, as we have to implement a recursive procedure for that. You'll call the changed sub DeleteOlderThan1day again:
Sub DeleteOlderThan1day()
  Call DeleteOlderInSub(Application.Session.Folder("pstname").Folders("Inbox").Folders("Fred"), DateAdd("d", -1, Now())
End Sub

Sub DeleteOlderInSub(oFolder as Folder, oldDate as Date)
Dim oSubfolder as Folder
Dim ItemsOverMonths As Outlook.Items

  Set ItemsOverMonths = oFolder.Items.Restrict("[Received] <= """ & Format(oldDAte, "mm/dd/yyyy") & """")
  For i = ItemsOverMonths.Count To 1 Step -1
    ItemsOverMonths.Item(i).Delete
  Next

  For Each oSubFolder In oFolder.Folders
    DeleteOlderInSub(oSubFolder, oldDate)
  Next

  Set ItemsOverMonths = Nothing
End Sub

Open in new window

Physimed

ASKER
I have a compilation error on thoses lines

Call DeleteOlderInSub(Application.Session.Folder("pstname").Folders("Inbox").Folders("Fred"), DateAdd("d", -1, Now())


DeleteOlderInSub(oSubFolder, oldDate)
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Physimed

ASKER
This is what I was expected

Thank you very much for your help
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck