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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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