1

I'm using Visual Studio to build an addin to copy emails.

The condition is to check, according to SentOn/ReceivedTime, and copy only those emails from the source folder that do not exist in the destination folder.

I tried below code but its gives me an error System.OutOfMemoryException Out of memory or system resources.

Sub CopyMail(SourceFolder As Outlook.Folder, DestinationFolder As Outlook.Folder)
    Dim sMail As Object
    Dim dMail As Object
    Dim MailC As Object
    For Each sMail In SourceFolder.Items
        For Each dMail In DestinationFolder.Items
            If sMail.SentOn <> dMail.SentOn Then
                MailC = sMail.Copy
                MailC.Move(DestinationFolder)
            End If
        Next
    Next
End Sub
Community
  • 1
  • 1
Hayat Hasan
  • 229
  • 2
  • 14
  • what is dFolder ? – jsotola Sep 09 '17 at 04:09
  • what happens when you single-step through the code? .... use F8 key – jsotola Sep 09 '17 at 04:13
  • @0m3r comparing according to `SentOn` time – Hayat Hasan Sep 09 '17 at 04:21
  • @jsotola outlook gets stuck after running F8/Debugging mode, (Actually im trying this vba to build an outlook add-in) – Hayat Hasan Sep 09 '17 at 04:28
  • what do you mean `stuck` ? .... do you mean it freezes ? ..... which line made it freeze? – jsotola Sep 09 '17 at 04:39
  • @jsotola whole outlook app is hangup/freezed , I can't even click on any button or menu, this is happening after I execute those above code ( I put those code under a button in outlook ribbon, using visual studio vsto) – Hayat Hasan Sep 09 '17 at 04:43
  • @jsotola it seem that e vba code is copying a single email in DestinationFolder multiple times like thousands of thousands times which might be causing the hangup issue, But why it is copying the same email over and over ? – Hayat Hasan Sep 09 '17 at 05:07
  • were you able to single-step the code? – jsotola Sep 09 '17 at 05:16
  • @jsotola all other code in the add-in is working fine, except only when this above code start executing and looping the copy function outlook gets hangup and only copying a single email over and over, – Hayat Hasan Sep 09 '17 at 05:20
  • @jsotola I pressed the F8 Key and it opened outlook in debugging mode and I executed the code which I've put under a button and then it shows nothing but hangup/freeze, even no error shown visual studio , – Hayat Hasan Sep 09 '17 at 05:35
  • It's not a good idea to modify a collection while you're in the process of looping over it. Use the For Each loop to add your items to a list/array, and then loop over that list/array and create/move the copies. – Tim Williams Sep 09 '17 at 05:38
  • i think that single-stepping code in visual studio is different from VBA ide. .... bottom line, you need to step the code one line at a time to find out which line causes the constant loop. ..... you also need to change your tags so that you can get help from visual studio people ..... the cause is probably as @TimWilliams has indicated – jsotola Sep 09 '17 at 05:50
  • Can you guys guide me by providing a sample code or hint at least to compare each email in two folder according to `SentOn` time . I'm googling a lot but seem to not finding any satisfactory hint or may be im looking in wrong way. Please guide me. – Hayat Hasan Sep 09 '17 at 05:56
  • Possible duplicate of [For Each loop: Some items get skipped when looping through Outlook mailbox to delete items](https://stackoverflow.com/questions/10725068/for-each-loop-some-items-get-skipped-when-looping-through-outlook-mailbox-to-de) – niton Sep 09 '17 at 14:47
  • @niton Thanks , but its not possible duplicate, – Hayat Hasan Sep 09 '17 at 14:50
  • Its a simple question as I asked for your kind assistance. above function suppose to return the emails that are not existed in 2nd folder using `If sMail.SentOn <> dMail.SentOn` but its returning each mail in 1st folder in multiple times , it could be a looping issue or anything I dont know. Please guide me. – Hayat Hasan Sep 09 '17 at 14:53
  • For Each is not appropriate when moving or deleting. Change to a reverse counting loop as described. – niton Sep 09 '17 at 14:58
  • how do I compare data from both folder using reverse counting loop – Hayat Hasan Sep 09 '17 at 15:01

1 Answers1

1

There is a logic error in your nested loop - for each item in the destination folder you copy all non-matches from the source folder, even though those items may match other items in the destination folder.

Here's an approach (untested) which should work. It's in VBA: my VB.NET is not good and anyway you tagged with VBA...

Sub CopyMail(SourceFolder As Outlook.Folder, DestinationFolder As Outlook.Folder)
    Dim sMail As Object
    Dim dMail As Object
    Dim MailC As Object
    Dim dictSent As New Scripting.dictionary, i As Long

    'get a list of all unique sent times in the
    '  destination folder
    For Each dMail In DestinationFolder.Items
        dictSent(dMail.SentOn) = True
    Next

    'loop through the source folder and copy all items where
    '  the sent time is not in the list
    For i = SourceFolder.Items.Count To 1 Step -1
        Set sMail = SourceFolder.Items(i)

        If Not dictSent.Exists(sMail.SentOn) Then
            Set MailC = sMail.Copy        'copy and move
            MailC.Move DestinationFolder
            dictSent(sMail.SentOn) = True 'add to list
        End If

    Next i

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Thanks a lot, I've tested your code and it primarily solved my purpose. In the means as I was reading all these comments and answers I get to think that would be enough to determine a mail as unique by filtering only `SentOn` . **How would I determine if a mail is unique considering that sometimes we get several emails with same subject or different emails in same time(seconds) ** – Hayat Hasan Sep 10 '17 at 04:18