Count Property (AddressEntries Collection)

The Count property returns the number of AddressEntry objects in the collection, or a very large number if the exact count is not available. Read-only.

Syntax

objAddrEntriesColl.Count

Data Type

Long

Remarks

A large collection cannot always maintain an accurate count of its members, and the Count property cannot be used as the collection's size when it has the value &H7FFFFFFF. Programmers needing to access individual objects in a large collection are strongly advised to use the Microsoft® Visual Basic® For Each statement or the Get methods.

The recommended procedures for traversing a large collection are, in decreasing order of preference:

  1. Global selection, such as the Visual Basic For Each statement.
  2. The Get methods, particularly GetFirst and GetNext.
  3. An indexed loop, such as the Visual Basic For ... Next construction.

If the address book provider cannot supply the precise number of AddressEntry objects, CDO returns &H7FFFFFFF ( = 2^31 – 1 = 2,147,483,647) for the Count property. This is the largest positive value for a long integer and is intended to prevent an approximate count from prematurely terminating an indexed loop. On 32-bit platforms, this value is defined in the type library as CdoMaxCount. On other platforms, CdoMaxCount is not defined, and a program on such a platform must compare the Count property against &H7FFFFFFF to see if it is reliable.

If the Count property is not reliable, that is, if it is &H7FFFFFFF, a program using it to terminate an indexed loop must also check each returned object for a value of Nothing to avoid going past the end of the collection.

The use of the Item property in conjunction with the Count property in a large collection can be seen in the following example.

Example

This code fragment counts the AddressEntry objects in a user's personal address book (PAB):

Dim indx As Long                ' loop index / object counter 
Dim myPAB as AddressList        ' personal address book AddressList 
Dim myPABColl as AddressEntries ' AddressEntries collection of PAB 
Dim objOneAE as AddressEntry    ' single address entry in collection 
' select PAB from AddressLists collection of Session 
Set myPAB = MAPI.Session.AddressLists.Item("Personal Address Book") 
' .Item could have been omitted above since it is default property 
' make sure returned AddressList object is valid 
If myPAB Is Nothing Then 
   ' MsgBox "PAB object is invalid" 
   ' Exit 
End If 
' get AddressEntries collection of PAB AddressList 
Set myPABColl = myPAB.AddressEntries 
' see if PAB is empty 
indx = myPABColl.Count ' valid if not a "very large number" 
If 0 = indx Then ' collection empty 
   MsgBox "No AddressEntry items in PAB" 
ElseIf CdoMaxCount = indx Then ' .Count is not valid; get exact count 
   indx = 0 ' set up to count individual address entries 
   For Each objOneAE in myPABColl 
      indx = indx + 1 ' another valid AddressEntry in collection 
   Next objOneAE 
End If