Link to home
Start Free TrialLog in
Avatar of Physimed
PhysimedFlag 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
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of 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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I was expected

Thank you very much for your help