Count Property (Messages Collection)

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

Syntax

objMsgColl.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 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 Message object with subject "Bonus". Note that the variable is declared as Object instead of Message, and that the Class property is tested to verify that the object returned in the Item property is not an AppointmentItem, GroupHeader, or MeetingItem object.

Dim i As Integer ' loop index / object counter 
Dim collMessages As Messages ' assume collection already provided 
Dim objMsg As Object ' could get either group header or message 
If collMessages Is Nothing Then 
    MsgBox "Messages collection object is invalid" 
    ' Exit 
ElseIf 0 = collMessages.Count Then ' collection is empty 
    MsgBox "No messages in collection" 
    ' Exit 
End If 
' look for message about "Bonus" in collection 
For i = 1 To collMessages.Count Step 1 
    Set objMsg = collMessages.Item(i) 
    ' or collMessages(i) since Item is default property 
    If objMsg Is Nothing Then ' end of collection 
        MsgBox "No such message found in collection" 
        Exit For 
    ElseIf objMsg.Class = CdoMsg Then ' exclude other object classes 
        If 0 = StrComp(objMsg.Subject, "Bonus") Then 
        ' or objMsg since Subject is default property 
            MsgBox "Desired message is at index " & i 
            Exit For 
        End If 
    End If 
Next i