Sauron
Sauron

Reputation: 6657

Removing Deleted Items given loose string matching on sender

I wish to create a VBA program to remove items within the Deleted Items folder of my outlook. However, I only wish to remove such items from the certain users through matching a loose string.

For example, deleting all emails within the Deleted Items box from any user with address like "Plan_Group_", given I may receive emails from "Plan_Group_1", "Plan_Group_2","Plan_Group_3",...etc.

At present this is what I have for deletion, but it is for all items within the Deleted Items box:

Sub RemoveAutomaticItemsInDeletedItems()
    Dim oDeletedItems As Outlook.Folder
    Dim oItems As Outlook.Items
    Dim i As Long
    'Obtain a reference to deleted items folder
    Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDeletedItems)
    Set oItems = oDeletedItems.Items
    For i = oItems.Count To 1 Step -1
        oItems.Item(i).Delete
    Next
End Sub

How can I extend this to only look for emails that loosely match a from address string?

Upvotes: 0

Views: 31

Answers (1)

SierraOscar
SierraOscar

Reputation: 17637

Use an If statement to check the email address:

If TypeName(oItems.Item(i)) = "MailItem" And oItems(i).SenderEmailAddress Like "Plan_Group_*" Then
    oItems.Item(i).Delete
End If

Or:

If TypeName(oItems.Item(i)) = "MailItem" And Left$(oItems(i).SenderEmailAddress, 11) = "Plan_Group_" Then
    oItems.Item(i).Delete
End If

Just 2 ways of doing it

Upvotes: 1

Related Questions