Hi all,
I would like to create a Macro in Excel through which I can delete all emails from all my folders in Outlook based on the sender email.
Currently I have the following code which allows me to select and delete emails from a specific sender email, but only allows me to select 1 folder at a time. Is it possible to adjust this and select multiple folders at a time (or all at once)?
Option Explicit
Public Sub Delete_Emails()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim FoldersList As Outlook.Folders
Dim olPurgeFolder As Outlook.MAPIFolder
Dim Items As Outlook.Items
Dim Filter As String
Dim Msg As String
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder
Filter = "[SenderEmailAddress] = 'SenderEmail@Address.com'"
Set Items = olPurgeFolder.Items.Restrict(Filter)
Msg = Items.Count & " items in " & olPurgeFolder.Name & ". Delete?"
If MsgBox(Msg, vbYesNo) = vbYes Then
For i = Items.Count To 1 Step -1
Debug.Print Items(i) 'Immediate Window
Items.Remove i
Next
End If
End Sub
I would like to create a Macro in Excel through which I can delete all emails from all my folders in Outlook based on the sender email.
Currently I have the following code which allows me to select and delete emails from a specific sender email, but only allows me to select 1 folder at a time. Is it possible to adjust this and select multiple folders at a time (or all at once)?
Option Explicit
Public Sub Delete_Emails()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim FoldersList As Outlook.Folders
Dim olPurgeFolder As Outlook.MAPIFolder
Dim Items As Outlook.Items
Dim Filter As String
Dim Msg As String
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olPurgeFolder = Outlook.GetNamespace("MAPI").PickFolder
Filter = "[SenderEmailAddress] = 'SenderEmail@Address.com'"
Set Items = olPurgeFolder.Items.Restrict(Filter)
Msg = Items.Count & " items in " & olPurgeFolder.Name & ". Delete?"
If MsgBox(Msg, vbYesNo) = vbYes Then
For i = Items.Count To 1 Step -1
Debug.Print Items(i) 'Immediate Window
Items.Remove i
Next
End If
End Sub