Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:46:01+00:00 2026-06-14T06:46:01+00:00

Description: I have an Outlook macro that loops through selected emails in a folder

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:46:03+00:00Added an answer on June 14, 2026 at 6:46 am

    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:

    For Each objItem In Application.ActiveExplorer.Selection
    

    you can loop through the parent folder. I thought I could do something like this:

    For Each objItem In oFolder.Items
    

    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:

    For i = oFolder.Items.Count To 1 Step -1 'Iterates from the end backwards
    Set objItem = oFolder.Items(i)
    

    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:”

    Sub SaveItemsToExcel()
        Debug.Print "Begin SaveItemsToExcel"
    
        Dim oNameSpace As Outlook.NameSpace
        Set oNameSpace = Application.GetNamespace("MAPI")
        Dim oFolder As Outlook.MAPIFolder
        Set oFolder = oNameSpace.PickFolder
        Dim IsFolderSpecialCase As Boolean
        Dim IsFolderOutofOffice As Boolean
        IsFolderSpecialCase = False
        IsFolderOutofOffice = False
    
        'If they don't check a folder, exit.
        If oFolder Is Nothing Then
            GoTo ErrorHandlerExit
        ElseIf oFolder.DefaultItemType <> olMailItem Then 'Make sure folder is not empty
            MsgBox "Folder does not contain mail messages"
            GoTo ErrorHandlerExit
        End If
    
        'Checks to see if Special Cases Folder and Out of Office folders exists. If not, create them
        For i = 1 To oFolder.Folders.Count
            If oFolder.Folders.Item(i).name = "Special Cases" Then IsFolderSpecialCase = True
            If oFolder.Folders.Item(i).name = "Out of Office" Then IsFolderOutofOffice = True
        Next
        If Not IsFolderSpecialCase Then oFolder.Folders.Add ("Special Cases")
        If Not IsFolderOutofOffice Then oFolder.Folders.Add ("Out of Office")
    
        'Asks user for name and location to save the export
        objOutputFile = CreateObject("Excel.application").GetSaveAsFilename(InitialFileName:="TestExport" & Format(Now, "_yyyymmdd"), fileFilter:="Outlook Message (*.csv), *.csv", Title:="Export data to:")
        If objOutputFile = False Then Exit Sub
        Debug.Print "   Will save to: " & objOutputFile & Chr(10)
    
        'Overwrite outputfile, with new headers.
        Open objOutputFile For Output As #1
        Print #1, "User ID,Last Name,First Name,Company Name,Subject,Vote Response,Recived"
    
        ProcessFolderItems oFolder, objOutputFile
    
        Close #1
    
        Set oFolder = Nothing
        Set oNameSpace = Nothing
        Set objOutputFile = Nothing
        Set objFS = Nothing
    
        MsgBox "All complete! Emails requiring attention are in the " & Chr(34) & "Special Cases" & Chr(34) & " subdirectory."
        Debug.Print "End SaveItemsToExcel."
        Exit Sub
    ErrorHandlerExit:
        Debug.Print "Error in code."
    End Sub
    
    Sub ProcessFolderItems(oParentFolder, ByRef objOutputFile)
        Dim oCount As Integer
        Dim oFolder As Outlook.MAPIFolder
        Dim MessageVar As String
        oCount = oParentFolder.Items.Count
        Dim CountVar As Integer
        Dim objItem As Outlook.MailItem
    
        CountVar = 0
    
        For i = oParentFolder.Items.Count To 1 Step -1 'Iterates from the end backwards
        Set objItem = oParentFolder.Items(i)
            DoEvents
            If objItem.Class = olMail Then
                If objItem.VotingResponse <> "" Then
                    CountVar = CountVar + 1
                    Debug.Print "   " & CountVar & ". " & GetUsername(objItem.SenderName, objItem.SenderEmailAddress) & "," & objItem.SenderName & "," & GetCompany(objItem.SenderName) & "," & Replace(objItem.Subject, ",", "") & "," & objItem.VotingResponse & "," & objItem.ReceivedTime
                    Print #1, GetUsername(objItem.SenderName, objItem.SenderEmailAddress) & "," & objItem.SenderName & "," & GetCompany(objItem.SenderName) & "," & Replace(objItem.Subject, ",", "") & "," & objItem.VotingResponse & "," & objItem.ReceivedTime
                ElseIf objItem.Subject Like "*Out of Office*" Then
                    CountVar = CountVar + 1
                    Debug.Print "   " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to the, " & Chr(34) & "Out of Office" & Chr(34) & " sub-folder"
                    objItem.Move oParentFolder.Folders("Out of Office")
                Else
                    CountVar = CountVar + 1
                    Debug.Print "   " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to the, " & Chr(34) & "Special Cases" & Chr(34) & " sub-folder"
                    objItem.Move oParentFolder.Folders("Special Cases")
                End If
            End If
        Next i
        Set objItem = Nothing
    End Sub
    
    Function GetUsername(SenderNameVar As String, SenderEmailVar As String) As String
        On Error Resume Next
        GetUsername = ""
        GetUsername = CreateObject("Outlook.Application").CreateItem(olMailItem).Recipients.Add(SenderNameVar).AddressEntry.GetExchangeUser.Alias
        If GetUsername = "" Then GetUsername = Mid(SenderEmailVar, InStrRev(SenderEmailVar, "=", -1) + 1)
    End Function
    
    Function GetCompany(SenderNameVar)
        On Error Resume Next
        GetCompany = ""
        GetCompany = CreateObject("Outlook.Application").CreateItem(olMailItem).Recipients.Add(SenderNameVar).AddressEntry.GetExchangeUser.CompanyName
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

trasaction table description field have some value it is worked fine. description feild default
I have an NStextView that loads a description from a JSON source. The NSTextView
I have an VSTO (outlook-addin) application that uses Unity. It is deployed to a
Description: I have a column(EmailsAdress) on a table(BusinessUsers) on my databases that stores email
i have description , title in a cell .. some of the title has
I have three headings and all three have some description. when i click on
DESCRIPTION I have two datasets with information that I need to merge. The only
Provide some tips to get rid of the following scenario. Description: I have two
My Description I have a website application that relies heavily on Javascript and JQuery.
Brief Description: I have a table that stores articles. Articles are listed on table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.