Question

Delete all Identical mails in a folder. Compare the Subject and then the body data of the mail and delete all except 1.

Asked by: bsharath

Hi,

Delete all Identical mails in a folder. Compare the Subject and then the body data of the mail and delete all except 1.
I receive mails those are Automatted mails from systems they are very offen identical with Subject and Body. Can i have a script to delete all the mails that might be identical.

REgards
Sharath

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-03-25 at 11:11:02ID24264376
Tags

Outlook

,

macro

Topics

Outlook Groupware Software

,

Microsoft Applications

,

Microsoft Office Suite

Participating Experts
2
Points
500
Comments
57

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. mailto: syntax ?subject= ?body= ???
    I know how to write a mailto: link which inserts a predefined string in the SUBJECT window with the syntax ?subject=... Is it possible to also insert a predefined string in the BODY window???
  2. Insert a subject and body to a mailto tag
    I know how to enter a mailto tag. but I would like the e-mail windoe to open with the subject field complete and some body. Can I do this and how?
  3. Rejecting messages with empty subject and body
    Is there a way in Sendmail to reject incoming messages with an empty subject and body. I have users who get these apparently spam 'probe' messages and it jams up their desktop AV client (namely Norton's) and I have to go delete the messages by hand. Since no real user would...
  4. Html code to create email with subject and body
    Hi Most machines in my organization use outlook express as the default email client. I want to create a link so that if i click on it, it will create an email with Microsoft outlook with subject and body. Thanks
  5. mailto: subject and body?
    Hi, Can someone tell me how to populate the subject and body of an email when someone clicks a mailto link. Thanks
  6. Is there a way to emulate mailto in Java with the subject an…
    Hi, Is there a way to emulate mailto in Java with the subject and body? I want to open up the default mail client with the To, Subject, and Body prefilled. Is this possible? Thanks Jamie

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: bruintjePosted on 2009-03-26 at 00:25:37ID: 23987942

Hello bsharath,

there is a little script posted here
http://www.outlookcode.com/d/code/movemaildupes.htm

i added the body to the script posted below in the check

hope this helps a bit
brian

Option Explicit
 
' Moves duplicate items from the Inbox to a Dupes folder
'   created under the Inbox
 
Private Sub MoveDuplicates()
  Dim myDate As Date
  Dim dupDate As Date
  Dim mySubject As String
  Dim dupSubject As String
  Dim mySender As String
  Dim dupSender As String
  Dim myItems As Items
  Dim myInbox As MAPIFolder
  Dim myItem As Object
  Dim dupItem As Object
  Dim myBody As String
  Dim dupBody As String
 
  Set myOlApp = CreateObject("Outlook.Application")
  Set myNameSpace = myOlApp.GetNamespace("MAPI")
  Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
  Set myItems = myInbox.Items
  On Error Resume Next
  Set dupsFolder = myInbox.Folders("Dupes")
  If Err <> 0 Then
     Set dupsFolder = myInbox.Folders.Add("Dupes")
     Err.Clear
  End If
 
  myItems.Sort "[Subject]", False
  Set myItem = myItems.GetFirst
  While TypeName(myItem) <> "Nothing"
    If TypeName(myItem) = "MailItem" Then
      myDate = myItem.ReceivedTime
      mySubject = myItem.Subject
      mySender = myItem.SenderName
      myBody = myItem.Body
      If Err = 0 Then
        Set dupItem = myItems.GetNext
        dupDate = dupItem.CreationTime
        If TypeName(dupItem) = "MailItem" Then
          dupSubject = dupItem.Subject
          dupSender = dupItem.SenderName
          dupBody = dupItem.Body
        End If
        If TypeName(dupItem) = "MailItem" _
          And mySubject = dupItem.Subject _
          And mySender = dupItem.SenderName _
          And myBody = dupItem.Body _
          And DateDiff("n", myDate, dupDate) < 2 Then
            dupItem.Move dupsFolder
        Else
          Set myItem = dupItem
        End If
      Else
        Err.Clear
        Set myItem = myItems.GetNext
      End If
    End If
  Wend
 
  Set myItem = Nothing
  Set dupItem = Nothing
  Set myItems = Nothing
  Set myInbox = Nothing
  Set myNameSpace = Nothing
  Set myOlApp = Nothing
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-26 at 04:42:46ID: 23989207

Thanks but i get this error

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Variable not defined
---------------------------
OK   Help  
---------------------------

I want them to be deleted and the ways to compare if its a duplicate is Subject & body data not just subject....

 

by: bsharathPosted on 2009-03-26 at 04:42:48ID: 23989208

Thanks but i get this error

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Variable not defined
---------------------------
OK   Help  
---------------------------

I want them to be deleted and the ways to compare if its a duplicate is Subject & body data not just subject....

 

by: chris_bottomleyPosted on 2009-03-28 at 11:09:11ID: 24009486

ONly within a specific folder?

Chris

 

by: bsharathPosted on 2009-03-28 at 11:10:36ID: 24009492

Yes within 1 folder and all sub folders within that folder alone...

 

by: chris_bottomleyPosted on 2009-03-28 at 12:41:29ID: 24009823

Nope sorry, I think I see a way to do it within a specific folder, (hence the question) but to do it for sub folders will be too difficult.

Chris

 

by: bsharathPosted on 2009-03-28 at 12:43:44ID: 24009834

Ok a single folder is fine. I can move the mails from subfolders to root folder...

 

by: chris_bottomleyPosted on 2009-03-28 at 14:58:30ID: 24010435

OK ... try this

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Outlook.mailitem
Dim strFilter As String
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    On Error GoTo skipthisone
    For mailCounter = myfolder.items.count To 1 Step -1
        Set mai = myfolder.items(mailCounter)
        strFilter = "[Subject] = " & append_quotes(mai.subject)
        Set olkmailitems = myfolder.items.Restrict(strFilter)
        For bodyCount = olkmailitems.count - 1 To 1 Step -1
            If mai.body = olkmailitems(bodyCount).body Then
                olkmailitems(bodyCount).Delete
            End If
        Next
skipthisone:
    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-29 at 01:38:28ID: 24011809

I get array index out of bound
When debug goes here
        Set mai = myfolder.Items(mailCounter)

 

by: bsharathPosted on 2009-03-29 at 01:38:30ID: 24011811

I get array index out of bound
When debug goes here
        Set mai = myfolder.Items(mailCounter)

 

by: bsharathPosted on 2009-03-29 at 02:27:50ID: 24011915

I am sure its working but get the error after deleting say 200 duplicate mails....

 

by: bsharathPosted on 2009-03-29 at 02:27:52ID: 24011916

I am sure its working but get the error after deleting say 200 duplicate mails....

 

by: chris_bottomleyPosted on 2009-03-29 at 02:50:32ID: 24011961

Again works fine in my test, thge onerror should take care of the fact that we are deleting emails quicjer than the main loop cycles.  I have modified the type to obj to see if it helps ... but the error code says it won't help however ...

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    On Error GoTo skipthisone
    For mailCounter = myfolder.items.count To 1 Step -1
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = myfolder.items.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                If mai.body = olkmailitems(bodyCount).body Then
                    olkmailitems(bodyCount).Delete
                End If
            Next
        End If
skipthisone:
    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-29 at 02:56:28ID: 24011977

It does work on a few mails after which i get the error

 

by: bsharathPosted on 2009-03-29 at 03:26:55ID: 24012071

I get this error too
the items have been moved or deleted.
I tried on my psts and on mailbox folder. Each folder i get this error or the other error which i mentioned above
When debug goes her
                If mai.Body = olkmailitems(bodyCount).Body Then

 

by: bruintjePosted on 2009-03-29 at 06:00:51ID: 24012505

ran into some quirks of the outlook inbox so i decided to try and understand that first, as i tested my original code and it ran over a copied item, even though it was sorted on subject but decided to look at creation date on an equal item

my code will only work on original items not on manually copied emails in the same folder

also picked up the subfolder from an earlier answer from Chris !!!
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_22995423.html

so if the solution posted below is working it's as much his work ;-)

brian

Sub launchpad()
 
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim MyFolder As Outlook.MAPIFolder
    On Error Resume Next
   
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set MyFolder = objNS.PickFolder
    Call ProcessFolder(MyFolder)
    Set objNS = Nothing
 
Set MyFolder = Nothing
Set olApp = Nothing
Set objNS = Nothing
 
End Sub
 
Sub ProcessFolder(StartFolder As MAPIFolder)
Dim objFolder As Outlook.MAPIFolder
Dim mySubject As String
Dim dupSubject As String
Dim myItems As Items
Dim myInbox As MAPIFolder
Dim myItem As Object
Dim dupItem As Object
Dim myBody As String
Dim dupBody As String
   
    On Error Resume Next
   
  Set myItems = StartFolder.Items
   
  myItems.Sort "[Subject]", False
  Set myItem = myItems.GetFirst
  While TypeName(myItem) <> "Nothing"
    If TypeName(myItem) = "MailItem" Then
      mySubject = myItem.Subject
      myBody = myItem.Body
      If Err = 0 Then
        Set dupItem = myItems.GetNext
        If TypeName(dupItem) = "MailItem" Then
          dupSubject = dupItem.Subject
          dupBody = dupItem.Body
          If mySubject = dupSubject And myBody = dupBody Then
            dupItem.Delete
          Else
            Set myItem = dupItem
          End If
        Else
          dupItem = myItems.GetNext
        End If
      Else
        Err.Clear
        Set myItem = myItems.GetNext
      End If
    Else
      Set myItem = myItems.GetNext
    End If
  Wend
       
  ' process all the subfolders of this folder
  For Each objFolder In StartFolder.Folders
      Call ProcessFolder(objFolder)
  Next
 
Set myInbox = Nothing
Set myItem = Nothing
Set dupItem = Nothing
Set objFolder = Nothing
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:

Select allOpen in new window

 

by: chris_bottomleyPosted on 2009-03-29 at 06:25:46ID: 24012598

OK, can't speak for Brians post but I have completed testing of a functional switch in method so it is as below.

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim intItems As Integer
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
    For mailCounter = myfolder.items.count To 1 Step -1
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = myfolder.items.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        intItems = intItems + 1
                    End If
                End If
            Next
        End If
skipthisone:
    Next
    For Each delItem In delItems.Keys
        If delItems(delItem) <> "" Then _
            Application.Session.GetItemFromID(delItems(delItem)).Delete
    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:

Select allOpen in new window

 

by: chris_bottomleyPosted on 2009-03-29 at 06:29:45ID: 24012617

Brian

Out of interest thank you ... I don't think in this case the reursion will work as it will treat sub folder in isolation so duplicates between subfolders will not be picked up ... and is the fundamental issue that concerned me over trying to work with email collections scattered over sub folders.

Nonetheless nice to see the code is understandable to someone else, I am trying to be consistent and structured ;o)

Chris

 

by: bruintjePosted on 2009-03-29 at 06:34:33ID: 24012646

>>duplicates between subfolders will not be picked up

yes Chris that is very true indeed, trying to implement that would be a little more complex as you already stated ;)

 

by: bsharathPosted on 2009-03-29 at 07:34:27ID: 24012849

Thank U both but what ways are the 2 scripts searching for Duplicates. Am not able to find what the script is taking into consideration....

 

by: bsharathPosted on 2009-03-29 at 07:34:30ID: 24012850

Thank U both but what ways are the 2 scripts searching for Duplicates. Am not able to find what the script is taking into consideration....

 

by: bruintjePosted on 2009-03-29 at 07:52:42ID: 24012924

the script from Chris, first sets all items to loop through

For mailCounter = myfolder.items.count To 1 Step -1

and filters on subject

next it checks the body
If mai.body = olkmailitems(bodyCount).body Then
               
if there is more than one occurence it marks the duplicates for deletion later

and that happens here
    For Each delItem In delItems.Keys
        If delItems(delItem) <> "" Then _
            Application.Session.GetItemFromID(delItems(delItem)).Delete
    Next

the script i worked with also starts with a box of items but it loops through them based on a sort by subject

next it deletes the duplicate when found and moves on to the next

        Set dupItem = myItems.GetNext
        If TypeName(dupItem) = "MailItem" Then
          dupSubject = dupItem.Subject
          dupBody = dupItem.Body
          If mySubject = dupSubject And myBody = dupBody Then
            dupItem.Delete
          Else
            Set myItem = dupItem
          End If
        Else
          dupItem = myItems.GetNext
        End If

it's a different kind of logic to do achieve the same result,

the first goes one time through them all marking duplicates based on a filter of subject and a check on the body, deleting them afterwards

the second goes through them all, deleting duplicates on its way making for a tricky loop, as you need to step over non-duplicates without losing the next unique item

hope i did not miss some insight here ;)

 

by: bsharathPosted on 2009-03-29 at 08:02:10ID: 24012956

Ok this means that Chris's script is what is right for me.

As Subject can be identical in lots of my mails and Body of the mail differ

So the script that checks the subject and then the body. If found identical delete all except 1.

 

by: bruintjePosted on 2009-03-29 at 08:10:39ID: 24012984

yes that should be it

 

by: chris_bottomleyPosted on 2009-03-29 at 08:20:37ID: 24013024

If one works then that is what you should of course use.  I got myself into no end of confusions during extended deletions none of which had the same error you were noting but an error is an error is an error!

I therefore changed my philosophy as noted by Brian to identify the duplicates but leave them alone.  In theis way the loop runs smotthly, BUT there will be a small, (very small) overhead to allow for the additional loop processing, therefore speed is offset for clarity.  The last act being that loop to delete the identified duplicates.

I emphasise however if both solutions work for you then you should accept the first viable solution posted, (i.e. Brians).

Chris

 

by: bsharathPosted on 2009-03-29 at 09:50:24ID: 24013400

Chris its been like 2hrs and the script is still running.....

 

by: bsharathPosted on 2009-03-29 at 09:50:26ID: 24013401

Chris its been like 2hrs and the script is still running.....

 

by: chris_bottomleyPosted on 2009-03-29 at 10:11:41ID: 24013497

Any reason there are an extreme number of mails ... teh nature of the remoded script shouldn't lock up as it is simply nested and sequential loops.  All the deletions that can cause problems have been isolated to a fixed loop so it should simply be running, if you want some feedback I can add it easily into the immediate window.  A processing overhead this time to gain some visibility

Chris

 

by: bsharathPosted on 2009-03-29 at 11:30:07ID: 24013876

Yes please...

 

by: chris_bottomleyPosted on 2009-03-29 at 11:47:39ID: 24013939

Okay, i've avoided anything sensitive so whatever you can provide should help identify the problem.

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim intItems As Integer
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
    Debug.Print "Items in folder at start is " & myfolder.items.count
    For mailCounter = myfolder.items.count To 1 Step -1
        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = myfolder.items.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        intItems = intItems + 1
                    End If
                End If
            Next
        End If
skipthisone:
    Next
    Debug.Print "Total items marked for deletion is " & delItems.count
    For Each delItem In delItems.Keys
        If delItems(delItem) <> "" Then
            Debug.Print "Deleting item " & delItems(delItem)
            Application.Session.GetItemFromID(delItems(delItem)).Delete
        End If
    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-30 at 10:46:54ID: 24021250

Chris it does work but its too slow.
When i put in 100 mails and run it finishes fast. But when i run on a 10,000 mails folder it takes like a ver very long time and i was not able to let it complete because of the Duration. Can this code be made any way faster than its now?

 

by: chris_bottomleyPosted on 2009-03-30 at 11:33:20ID: 24021747

First off are you talking the one with debug in ... because removing the outputs, (i.e. the previous post)  should speed it up some.

Chris

 

by: chris_bottomleyPosted on 2009-03-30 at 11:48:13ID: 24021925

I think a redesign may help a bit, to allow for the deleted items but other than that then unless there are hundreds of emails with the same subject but different bodies then there is little (that I see) that can be done.

On the other hand it can be modified to process for example a selected number of mails at a time to return control quicker?

Chris

 

by: chris_bottomleyPosted on 2009-03-30 at 12:01:31ID: 24022073

I've had a try at a restructure, limited amount of testing done ... but it looks ok ;o)

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 1
'    For mailCounter = myfolder.items.count To 1 Step -1
        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = sortedList.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
    Debug.Print "Total items marked for deletion is " & delItems.count
    For Each delItem In delItems.Keys
        If delItems(delItem) <> "" Then
            Debug.Print "Deleting item " & delItems(delItem)
            Application.Session.GetItemFromID(delItems(delItem)).Delete
        End If
    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-30 at 20:29:51ID: 24025515

Chris the last code is fast but it checks each mail with all the mails and thats going to take days. As i have 80,000 mails that i need to process each week.

So one idea though. I am sure you would have thought but from my side.
Can the cpde check the mail if found duplicate delete it thesec it finds. So the next compare will have a reduced bunch of mails to check.

 

by: bsharathPosted on 2009-03-30 at 20:29:53ID: 24025516

Chris the last code is fast but it checks each mail with all the mails and thats going to take days. As i have 80,000 mails that i need to process each week.

So one idea though. I am sure you would have thought but from my side.
Can the cpde check the mail if found duplicate delete it thesec it finds. So the next compare will have a reduced bunch of mails to check.

 

by: chris_bottomleyPosted on 2009-03-30 at 22:23:50ID: 24025896

I question your comment about against each email, i've checked and only see one minor bug as fixed below.

I sort the emails at the start and filter for the mails in turn ... from end to start as it's still easier then for each email steadily decrements whilst no duplication is found, once dif a duplicate is found it also decrements for each duplicate meaning that it ought to process each mail just the once and is the only way I can see to speed it up ... why do you think it checks each mail with each other mail?

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 0
'    For mailCounter = myfolder.items.count To 1 Step -1
        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = sortedList.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
    Debug.Print "Total items marked for deletion is " & delItems.count
    For Each delItem In delItems.Keys
        If delItems(delItem) <> "" Then
            Debug.Print "Deleting item " & delItems(delItem)
            Application.Session.GetItemFromID(delItems(delItem)).Delete
        End If
    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-30 at 23:02:21ID: 24026040

This code is fast say on 1000 mails ... When i put 1000's it take a while...
Just wanted to know. Is there any way to delete the mail as and when it finds a duplicate. Why i ask is i could break the macro when ever i need and run it again when i am free. This way i can work as well as run the code...

 

by: bsharathPosted on 2009-03-30 at 23:02:23ID: 24026041

This code is fast say on 1000 mails ... When i put 1000's it take a while...
Just wanted to know. Is there any way to delete the mail as and when it finds a duplicate. Why i ask is i could break the macro when ever i need and run it again when i am free. This way i can work as well as run the code...

 

by: chris_bottomleyPosted on 2009-03-30 at 23:15:20ID: 24026100

Should be viable, be right back

Chris

 

by: chris_bottomleyPosted on 2009-03-30 at 23:19:58ID: 24026118

Try the following

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
'    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 0
'    For mailCounter = myfolder.items.count To 1 Step -1
'        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = sortedList.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
'                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
'                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        olkmailitems(bodyCount).Delete
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
'    Debug.Print "Total items marked for deletion is " & delItems.count
'    For Each delItem In delItems.Keys
'        If delItems(delItem) <> "" Then
'            Debug.Print "Deleting item " & delItems(delItem)
'            Application.Session.GetItemFromID(delItems(delItem)).Delete
'        End If
'    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-30 at 23:42:56ID: 24026242

I do get the mails deleted faster
But get this
The item has been moved or deleted
When debug goes here
 If mai.Body = olkmailitems(bodyCount).Body Then

 

by: bsharathPosted on 2009-03-30 at 23:42:58ID: 24026243

I do get the mails deleted faster
But get this
The item has been moved or deleted
When debug goes here
 If mai.Body = olkmailitems(bodyCount).Body Then

 

by: chris_bottomleyPosted on 2009-03-31 at 00:35:49ID: 24026493

I don't understand how even the quick test I did should work and yet fail for you.  I can however see a bug which I don't think is to blame but is addressed below:

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
'    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 0
'    For mailCounter = myfolder.items.count To 1 Step -1
'        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = sortedList.Restrict(strFilter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                If delItems.count <> 0 Then delItems.RemoveAll
'                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
'                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        olkmailitems(bodyCount).Delete
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
'    Debug.Print "Total items marked for deletion is " & delItems.count
'    For Each delItem In delItems.Keys
'        If delItems(delItem) <> "" Then
'            Debug.Print "Deleting item " & delItems(delItem)
'            Application.Session.GetItemFromID(delItems(delItem)).Delete
'        End If
'    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-31 at 01:21:29ID: 24026700

get this
The item has been moved or deleted
When debug goes here
 If mai.Body = olkmailitems(bodyCount).Body Then

 

by: bsharathPosted on 2009-03-31 at 01:21:31ID: 24026701

get this
The item has been moved or deleted
When debug goes here
 If mai.Body = olkmailitems(bodyCount).Body Then

 

by: chris_bottomleyPosted on 2009-03-31 at 01:44:50ID: 24026815

Still don't understand BUT all of my test data has been the same date so the following addresses what may be a result of a different ordering in the collection.

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
'    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 0
'    For mailCounter = myfolder.items.count To 1 Step -1
'        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = sortedList.Restrict(strFilter)
            Set mai = olkmailitems.items(mailCounter)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                If delItems.count <> 0 Then delItems.RemoveAll
'                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
'                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        olkmailitems(bodyCount).Delete
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
'    Debug.Print "Total items marked for deletion is " & delItems.count
'    For Each delItem In delItems.Keys
'        If delItems(delItem) <> "" Then
'            Debug.Print "Deleting item " & delItems(delItem)
'            Application.Session.GetItemFromID(delItems(delItem)).Delete
'        End If
'    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: chris_bottomleyPosted on 2009-03-31 at 01:58:46ID: 24026888

Bear with me an error in the change undergoing test the now

Chris

 

by: chris_bottomleyPosted on 2009-03-31 at 02:04:36ID: 24026910

Retested as follows:

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
'    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 0
'    For mailCounter = myfolder.items.count To 1 Step -1
'        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[Subject] = " & append_quotes(mai.subject)
            Set olkmailitems = sortedList.Restrict(strFilter)
            Set mai = olkmailitems(olkmailitems.count)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                If delItems.count <> 0 Then delItems.RemoveAll
'                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
'                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        olkmailitems(bodyCount).Delete
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
'    Debug.Print "Total items marked for deletion is " & delItems.count
'    For Each delItem In delItems.Keys
'        If delItems(delItem) <> "" Then
'            Debug.Print "Deleting item " & delItems(delItem)
'            Application.Session.GetItemFromID(delItems(delItem)).Delete
'        End If
'    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: bsharathPosted on 2009-03-31 at 08:42:34ID: 24030123

Chris it does work i got say 9 folders moved to the inbox.
Now my Q is did it scan all the folders and sub folders.
And how does it consider it to be a duplicate
1. Is case a matter
2. Will the first word or all words be considered.
3. Is any depth checked like

inbox
>Sharath
>> Ramesh
>>> Sharath
Is this also checked.

 

by: bsharathPosted on 2009-03-31 at 09:05:20ID: 24030419

Please omit the above comments

 

by: chris_bottomleyPosted on 2009-03-31 at 10:26:54ID: 24031327

?

 

by: bsharathPosted on 2009-03-31 at 10:28:42ID: 24031352

Please omit the above. I have sent you an email about the mails that does not get deleted...

its slow but works fine expect some issues...

 

by: chris_bottomleyPosted on 2009-03-31 at 15:03:51ID: 24034186

I have made a small change that may help ... they all deleted from my folder when I tried!

In order to put them into a folder i forwarded them to myself which reminded me to use the conversation topic instead of the subject.  See if affects those emails for you.

Chris

Sub deleteExcessEmails()
Dim olkApp As Outlook.Application
Dim objns As Outlook.NameSpace
Dim olkmailitems As Outlook.items
Dim myfolder As Object
Dim mailCounter As Integer
Dim bodyCount As Integer
Dim mai As Object
Dim strFilter As String
Dim delItems As Object
Dim delItem As Variant
Dim sortedList As Object
    
    Set olkApp = Outlook.Application
    Set objns = olkApp.GetNamespace("MAPI")
    Set myfolder = olkApp.ActiveExplorer.CurrentFolder
    Set delItems = CreateObject("scripting.dictionary")
    On Error GoTo skipthisone
'    Debug.Print "Items in folder at start is " & myfolder.items.count
    Set sortedList = myfolder.items
    sortedList.Sort "[Subject]", olAscending
    mailCounter = sortedList.count
    Do While mailCounter > 0
'    For mailCounter = myfolder.items.count To 1 Step -1
'        Debug.Print "Processing mail item " & mailCounter & " of " & myfolder.items.count & " in the folder."
        Set mai = myfolder.items(mailCounter)
        If mai.Class = olMail Then
            strFilter = "[conversationtopic] = " & append_quotes(mai.ConversationTopic)
            Set olkmailitems = sortedList.Restrict(strFilter)
            Set mai = olkmailitems(olkmailitems.count)
            For bodyCount = olkmailitems.count - 1 To 1 Step -1
                If delItems.count <> 0 Then delItems.RemoveAll
'                Debug.Print "Filtered items found is " & bodyCount
                If mai.body = olkmailitems(bodyCount).body Then
                    If Not delItems.Exists(olkmailitems(bodyCount).EntryID) Then
'                        Debug.Print "New item entered into delitems list."
                        delItems.Add Key:=olkmailitems(bodyCount).EntryID, Item:=olkmailitems(bodyCount).EntryID
                        olkmailitems(bodyCount).Delete
                        mailCounter = mailCounter - 1
                    End If
                End If
            Next
        End If
        mailCounter = mailCounter - 1
skipthisone:
    Loop
'    Debug.Print "Total items marked for deletion is " & delItems.count
'    For Each delItem In delItems.Keys
'        If delItems(delItem) <> "" Then
'            Debug.Print "Deleting item " & delItems(delItem)
'            Application.Session.GetItemFromID(delItems(delItem)).Delete
'        End If
'    Next
    
Set olkmailitems = Nothing
Set objns = Nothing
Set olkApp = Nothing
Set myfolder = Nothing
 
End Sub
 
Function append_quotes(objString As String) As String
    append_quotes = Chr(34) & CStr(objString) & Chr(34)
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:

Select allOpen in new window

 

by: chris_bottomleyPosted on 2009-04-01 at 23:23:35ID: 24046915

Any update?

Chris

 

by: bsharathPosted on 2009-04-01 at 23:45:49ID: 24047050

Thanks a lot Chris it does work fine but a bit slow. It took nearly 12+ hrs to check and complete 30,000 emailos. But it does work and solves my requirment....
:-))

 

by: bsharathPosted on 2009-04-01 at 23:45:51ID: 24047051

Thanks a lot Chris it does work fine but a bit slow. It took nearly 12+ hrs to check and complete 30,000 emailos. But it does work and solves my requirment....
:-))

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...