Article ID: 195699 - Last Review: May 20, 2002 - Revision: 1.1 OL98: How to Properly Loop Through Items in a CollectionThis article was previously published under Q195699 On This PageMORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either
expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes
that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can
help explain the functionality of a particular procedure, but they will not
modify these examples to provide added functionality or construct procedures to
meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites: Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice) For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) Properly Referencing Members of a CollectionIf you use Visual Basic or Visual Basic for Applications code that loops through items in a folder, you may see the modifications to those items are not saved. You may be unintentionally re-retrieving the item from the Items collection, and any changes that you have made to an item are unexpectedly lost.Many Outlook solutions modify the contents of items in a folder. In most scenarios, you loop through the Items collection in the Outlook object model. If you do not properly reference the items in the collection, you may receive unexpected results. Before modifying an item and saving it, you should set an object variable to the item, make changes to the item using the object variable, and then save the object. Consider the following Visual Basic automation code sample, which is designed to reset the birthday field for each contact in the default Contacts folder: NOTE: Be sure to reference the Outlook 98 Object Library before running these code examples and be aware running this code will modify any existing contacts you have in your Contacts folder. The following example is one way of modifying the previous code sample, so that it executes as expected: The following example provides the same functionality as the previous example, but uses the For Each...Next structure to loop through the items: Deleting All Members of a CollectionIf you want to programmatically delete all of the members of a collection, there are a few approaches that will work but there are also a number of approaches that will not work. Unexpected results occur because a collection is changing as you delete members from within it and the collection is not update dynamically. Typically you will find that every other item in the collection is deleted.The following automation code example exhibits this behavior. Before running this code, create a subfolder of your Inbox called Test and copy (not move) some items into the folder so that they can be deleted. Handling Unexpected Item TypesIf you are looping through items in a folder, you should make sure your solution will work even if the folder contains items that you might not expect to be there. The Inbox typically poses the most concern since the user generally has less control over what items are placed in that folder.Examples of items that you might unexpectedly find in a folder include:
Following are approaches you can use to avoid these types of problems. Choose the approach that is best suited to your solution, the type of folder you are working with and the types of items that could potentially affect your solution.
REFERENCES
For more information about creating solutions with Microsoft Outlook 98,
please see the following articles in the Microsoft Knowledge Base:
180826
(http://support.microsoft.com/kb/180826/EN-US/
)
OL98: Resources for Custom Forms and Programming
182349
(http://support.microsoft.com/kb/182349/EN-US/
)
OL98: Questions About Custom Forms and Outlook Solutions
SUMMARY
This article describes common scenarios and considerations to take into
account when programming with collections. The following topics are
covered:
| Article Translations
|

Back to the top
