ouch_mybrain_
asked on
Outlook VBA Code - Delete Items in Sub Folder in additional mailbox when Outlook Opens (Code provided so far)
Hello Experts,
I have found a pre-written macro for Outlook (VBA) which when run will allow me to pick a folder and then delete items older than 6 months (which I have since changed to 1 day) from the picked folder.
The folder I want to delete the items in is in an additional mailbox; I can select it just fine using the "Application.Session.PickF older" function.
I want to be able to change the pick folder function into something more static. I know the folder I want to delete the items from, infact this folder won't change. So I would like to replace the function with a reference to the location of the folder. I imagine it would be something like "mailbox.[mailboxname].[su bfolder]". But I really am not sure.
Can someone help provide me with the code I need to put into this part? The code is below:
I also intend to make this sub run as part of Outlook being opened, but I think i will be OK with this part.
Thanks.
I have found a pre-written macro for Outlook (VBA) which when run will allow me to pick a folder and then delete items older than 6 months (which I have since changed to 1 day) from the picked folder.
The folder I want to delete the items in is in an additional mailbox; I can select it just fine using the "Application.Session.PickF
I want to be able to change the pick folder function into something more static. I know the folder I want to delete the items from, infact this folder won't change. So I would like to replace the function with a reference to the location of the folder. I imagine it would be something like "mailbox.[mailboxname].[su
Can someone help provide me with the code I need to put into this part? The code is below:
Sub DeleteOlderThan1day()
Dim oFolder As Folder
Dim Date1day As Date
Dim ItemsOverMonths As Outlook.Items
Dim DateToCheck As String
Date1day = DateAdd("d", -1, Now())
Date1day = Format(Date1day, "mm/dd/yyyy")
Set oFolder = Application.Session.PickFolder 'or set your folder
DateToCheck = "[Received] <= """ & Date1day & """"
Set ItemsOverMonths = oFolder.Items.Restrict(DateToCheck)
For i = ItemsOverMonths.Count To 1 Step -1
ItemsOverMonths.Item(i).Delete
Next
Set ItemsOverMonths = Nothing
Set oFolder = Nothing
End Sub
I also intend to make this sub run as part of Outlook being opened, but I think i will be OK with this part.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER