Description:
I have an Outlook macro that loops through selected emails in a folder and writes down some info to a .csv file. It works perfectly up until 250 before failing. Here is some of the code:
Open strSaveAsFilename For Append As #1
CountVar = 0
For Each objItem In Application.ActiveExplorer.Selection
DoEvents
If objItem.VotingResponse <> "" Then
CountVar = CountVar + 1
Debug.Print " " & CountVar & ". " & objItem.SenderName
Print #1, & objItem.SenderName & "," & objItem.VotingResponse
Else
CountVar = CountVar + 1
Debug.Print " " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to: Special Cases sub-folder"
objItem.Move CurrentFolderVar.Folders("Special Cases")
End If
Next
Close #1
Problem
After this code runs through 250 emails, the following screenshot pops up:
https://i.stack.imgur.com/yt9P8.jpg
I’ve tried adding a “wait” function to give the server a rest so that I’m not querying it so quickly, but I get the same error at the same point.
Thanks to @76mel, for his answer to another question which I referenced heavily. I found out that it is a built-in limitation in Outlook (source) that you can’t open more than 250 items and Outlook keeps them all in memory until the macro ends no matter what. The workaround, instead of looping through each item in selection:
you can loop through the parent folder. I thought I could do something like this:
but, it turns out that when you delete or move an email, it shifts the list up one, so it will skip emails. The best way to iterate through a folder that I found in another answer is to do this:
Here is the whole code, which prompts for a folder to choose to parse, creates sub-directories in that folder for “Out of Office” replies as well as “Special Cases” where it puts all emails that begin with “RE:”