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?
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