Count Property (Folders Collection)

Count Property (Folders Collection)

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

Syntax

objFoldersColl.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 message store provider cannot supply the precise number of Folder 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 searches for a Folder object called "Resumes":

Dim i As Integer ' loop index / object counter
Dim collFolders as Folders ' Folders collection; assume already given
If collFolders Is Nothing Then
    ' MsgBox "Folders collection object is invalid"
    ' Exit
End If
' see if collection is empty
If 0 = collFolders.Count Then
    ' MsgBox "No folders in collection"
    ' Exit
End If
' look for folder called "Resumes" in collection
For i = 1 To collFolders.Count Step 1
    If collFolders.Item(i) Is Nothing Then
    ' MsgBox "No such folder found in collection"
    ' Exit ' no more folders in collection
    End If
    If collFolders.Item(i).Name = "Resumes" Then
    ' MsgBox "Desired folder is at index " & i
    ' Exit
    End If
Next i
 

See Also

Concepts

Folders Collection Object