For Each routine doesn't work

G

Greg Maxey

Can someone explain why the following snippet of code fails to delete all of the list entries in a dropdown field:

Sub Test()
Dim oFFld As FormFields
Dim oLe As ListEntry
Set oFFld = ActiveDocument.FormFields
For Each oLe In oFFld("Dropdown1").DropDown.ListEntries
oLe.Delete
Next
End Sub

It seems to always leave one or tow list entries in the dropdown list

The following code seems to take care of all of them, but I am curious as to why the above code won't work:

For i = oFFld("Dropdown2").DropDown.ListEntries.Count To 1 Step -1
oFFld("Dropdown2").DropDown.ListEntries(i).Delete
Next

Thanks
 
J

Jezebel

Some collections are mismanaged internally. The act of deleting an item from
the collection seems to interfere with the iteration of the collection. It
seems that the internal list pointers get scrambled. (Notwithstanding the MS
documentation that claims the for ... each will always iterate every item in
the collection, whatever changes might happen on the way through.) I've not
researched it, but I get the impression it's related to the ordering within
the collection -- that is, if the collection ordering is not the order in
which the items were added -- the internal equivalent of using the Before or
After arguments when you add items to the collection.

Another construction you can use --

Do until oFFld("Dropdown2").DropDown.ListEntries.Count = 0
oFFld("Dropdown2").DropDown.ListEntries(1).Delete
Loop



Can someone explain why the following snippet of code fails to delete all of
the list entries in a dropdown field:

Sub Test()
Dim oFFld As FormFields
Dim oLe As ListEntry
Set oFFld = ActiveDocument.FormFields
For Each oLe In oFFld("Dropdown1").DropDown.ListEntries
oLe.Delete
Next
End Sub

It seems to always leave one or tow list entries in the dropdown list

The following code seems to take care of all of them, but I am curious as to
why the above code won't work:

For i = oFFld("Dropdown2").DropDown.ListEntries.Count To 1 Step -1
oFFld("Dropdown2").DropDown.ListEntries(i).Delete
Next

Thanks
 
G

Greg Maxey

Jezebel,

Sounds like a reasonable explanation and thanks for jogging my memory about
the other method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top