none
Problem with loop all the emails inside the outlook folder and sub-folder RRS feed

  • Question

  • Hello,

    I try to loop all the emails inside the outlook folder and sub-folder.
    The following code displays an error at run time:(This is probably meant because my software is not in English.)

    The server administrator has limited the number of items that can be opened at the same time.
    Try closing the open message or deleting attachments and images from the unsent messages you are composing.

    Please help. Thanks.

        Sub LoopMails()
            Dim objOL As Outlook.Application
            Dim objNS As Outlook.NameSpace
            Dim objFolder As Outlook.Folder
    
            objOL = New Outlook.Application
            objNS = objOL.GetNamespace("MAPI")
            For Each objFolder In objNS.Folders
                Call LoopFolders(objFolder)
            Next
    
            objOL = Nothing
            objNS = Nothing
            objFolder = Nothing
        End Sub
    
        Sub LoopFolders(ByVal startFolder As Outlook.Folder)
            Dim objFolder As Outlook.Folder
    
            For Each itm In startFolder.Items
                If TypeOf itm Is Outlook.MailItem Then
                    Dim oMail As Outlook.MailItem
                    oMail = itm
                    If Not IsNothing(oMail.Subject) And Not IsNothing(oMail.SenderEmailAddress) Then 'The error occurred here.
                        'do some thing
                    End If
                    oMail = Nothing
                End If
            Next
    
            If startFolder.Folders.Count > 0 Then
                For Each objFolder In startFolder.Folders
                    Call LoopFolders(objFolder)
                Next
            End If
    
            objFolder = Nothing
        End Sub

    Thursday, February 2, 2017 8:32 AM

Answers

  • Hello Leon,

     You get a message because your code attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. See Systematically Releasing Objects for more information about releasing COM objects. Or you need to contact Exchange administrator to adjust the max number items that can be opened. See Server Administrator has Limited the Number of Items You Can Open Simultaneously .

    Looping through all emails is a time consuming task. Why do you need to loop over all items in Outlook?

    Note, you can use the Find/FindNext or Restrict methods of the Items class to find items that correspond to your conditions without iterating through all items. Read more about these methods in the following articles:

    How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)

    How To: Use Restrict method to retrieve Outlook mail items from a folder

    Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:

     - The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
     - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
     - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
     - You can stop the search process at any moment using the Stop method of the Search class.

    See Advanced search in Outlook programmatically: C#, VB.NET for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by leon1526 Thursday, February 2, 2017 1:06 PM
    Thursday, February 2, 2017 11:24 AM
  • Yes, you can - take a look at the Chr and ChrW functions available in VBA. The ci_startwith and ci_phrasematch  content indexer keywords provide prefix and phrase matching respectively for items in an indexed store.  See Filtering Items Using Query Keywords for more information. 

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by leon1526 Friday, February 17, 2017 2:24 AM
    Thursday, February 16, 2017 12:45 PM

All replies

  • Hello Leon,

     You get a message because your code attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. See Systematically Releasing Objects for more information about releasing COM objects. Or you need to contact Exchange administrator to adjust the max number items that can be opened. See Server Administrator has Limited the Number of Items You Can Open Simultaneously .

    Looping through all emails is a time consuming task. Why do you need to loop over all items in Outlook?

    Note, you can use the Find/FindNext or Restrict methods of the Items class to find items that correspond to your conditions without iterating through all items. Read more about these methods in the following articles:

    How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)

    How To: Use Restrict method to retrieve Outlook mail items from a folder

    Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:

     - The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
     - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
     - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
     - You can stop the search process at any moment using the Stop method of the Search class.

    See Advanced search in Outlook programmatically: C#, VB.NET for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by leon1526 Thursday, February 2, 2017 1:06 PM
    Thursday, February 2, 2017 11:24 AM
  • Hi Eugene,

    I modified the above code based on your advice. Now the program can run.
    But, as you said, looping through all emails is a time consuming task.
    Therefore, I have to give up the solution and try other solutions you mentioned. Thank you for your professional reply! Thank you!

    Thursday, February 2, 2017 1:06 PM
  • Note, you can use the Find/FindNext or Restrict methods of the Items class to find items that correspond to your conditions without iterating through all items.
    I noted when filtering text fields, the criteria must be completely equal, for example:
    sFilter = "[CompanyName] = 'Microsoft'"
    Can I use Find/FindNext or Restrict methods to determine if the text fields contain a special symbol? For example, I would like to find out if the recipient's address exists "@". 

    Wednesday, February 15, 2017 10:29 AM
  • Yes, you can - take a look at the Chr and ChrW functions available in VBA. The ci_startwith and ci_phrasematch  content indexer keywords provide prefix and phrase matching respectively for items in an indexed store.  See Filtering Items Using Query Keywords for more information. 

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by leon1526 Friday, February 17, 2017 2:24 AM
    Thursday, February 16, 2017 12:45 PM