Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

For Each routine doesn't work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 23 Mar 2006 00:11 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jezebel - 23 Mar 2006 01:37 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Greg Maxey - 23 Mar 2006 01:55 GMT
Jezebel,

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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Some collections are mismanaged internally. The act of deleting an
> item from the collection seems to interfere with the iteration of the
[quoted text clipped - 34 lines]
>
> Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.