Requested Member of the Collection Does Not Exist

Microsoft Word Visual Basic

Requested Member of the Collection Does Not Exist

   

The "requested member of the collection does not exist" error occurs when you try to access an object that doesn't exist. For example, the following instruction may post an error if the active document doesn't contain at least one table.

Sub SelectTable()
    ActiveDocument.Tables(1).Select
End Sub

To avoid this error when accessing a member of a collection, ensure that the member exists prior to accessing the collection member. If you're accessing the member by index number, you can use the Count property to determine if the member exists. The following example selects the first table if there is at least one table in the active document.

Sub SelectFirstTable()
    If ActiveDocument.Tables.Count > 0 Then
        ActiveDocument.Tables(1).Select
    Else
        MsgBox "Document doesn't contain a table"
    End If
End Sub

If you're accessing a collection member by name, you can loop on the elements in a collection using a For Each...Next loop to determine if the named member is part of the collection. For example, the following example deletes the AutoCorrect entry named "acheive" if it's part of the AutoCorrectEntries collection. For more information, see Looping Through a Collection.

Sub DeleteAutoTextEntry()
    Dim aceEntry As AutoCorrectEntry
    For Each aceEntry In AutoCorrect.Entries
        If aceEntry.Name = "acheive" Then aceEntry.Delete
    Next aceEntry
End Sub