Delete all emails from all folders in Outlook by Sender

Disco10

New Member
Joined
Jul 6, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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
The Application.AdvancedSearch method (Outlook) allows you to search multiple folders for items.
 
Upvote 0

Forum statistics

Threads
1,223,877
Messages
6,175,138
Members
452,614
Latest member
MRSWIN2709

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top