Macro to delete emails from specific senders is not working

musicgold

Board Regular
Joined
Jan 9, 2008
Messages
197
Hi,

I use Outlook 2010 and am trying to create a macro to delete emails from specific senders. See my code below.
For some reason the code doesn't loop through all emails in my inbox; it just go through a first few emails. Why is it happening?

Code:
Dim oloutlook As Outlook.Application
Dim ns As Outlook.NameSpace
Dim itm As Outlook.Items
Dim Myitem As MailItem
Dim counter As Integer
Dim olTrash As Object
Dim msg As String

On Error Resume Next

Set oloutlook = CreateObject("Outlook.Application")
Set ns = oloutlook.GetNamespace("MAPI")
Set itm = ns.GetDefaultFolder(olFolderInbox)
Set olTrash = ns.GetDefaultFolder(olFolderDeletedItems)

MsgBox itm.Items.Count
counter = 0

For Each Myitem In itm.Items
        
   Select Case UCase(Myitem.SenderName)
       
   Case UCase("Adam Berry")
         Myitem.Move olTrash
         counter = counter + 1
        
   Case UCase("John Nash")
         Myitem.Move olTrash
         counter = counter + 1
         
   End Select
 
 Next

Set oloutlook = Nothing
Set ns = Nothing
Set itm = Nothing

MsgBox "deleted " & counter
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
its your looping through the mails whenever a mail gets deleted the next one will be skipped

you have to loop backwards through the mails like

Code:
<code>For i = Inbox.Items.Count To 1 Step -1 'Iterates from the end backwards
    Set InboxMsg = Inbox.Items(i)</code></pre>
 
Upvote 0
the for loop combined

Code:
For i = Inbox.Items.Count To 1 Step -1 'Iterates from the end backwards
    Set InboxMsg = Inbox.Items(i)
        
   Select Case UCase(InboxMsg.SenderName)
       
   Case UCase("Adam Berry")
         Myitem.Move olTrash
         counter = counter + 1
        
   Case UCase("John Nash")
         Myitem.Move olTrash
         counter = counter + 1
         
   End Select
 
 Next
 
Upvote 0
If anyone is looking to use this, Ive compiled it into a complete working macro as it took some adjustments to get it to work, here's the full code:

Code:
Dim oloutlook As Outlook.Application
Dim ns As Outlook.NameSpace
Dim itm As Object
Dim Myitem As MailItem
Dim counter As Integer
Dim MailNumber As Long
Dim i As Long
Dim olTrash As Object

On Error Resume Next

Set oloutlook = CreateObject("Outlook.Application")
Set ns = oloutlook.GetNamespace("MAPI")
Set itm = ns.GetDefaultFolder(olFolderInbox)
Set olTrash = ns.GetDefaultFolder(olFolderDeletedItems)

MailNumber = itm.Items.count
counter = 0


For i = itm.Items.count To 1 Step -1 'Iterates from the end backwards
    Set Myitem = itm.Items(i)
        
   Select Case UCase(Myitem.SenderName)
       
   Case UCase("Adam Berry")
         Myitem.Move olTrash
         counter = counter + 1
         
   Case UCase("John Nash")
         Myitem.Move olTrash
         counter = counter + 1
   End Select
 
 Next
 

Set oloutlook = Nothing
Set ns = Nothing
Set itm = Nothing

MsgBox "Deleted " & counter & " of " & MailNumber
 
Upvote 0

Forum statistics

Threads
1,215,632
Messages
6,125,908
Members
449,273
Latest member
mrcsbenson

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