FP2000: For Each Loop Skips Every Other Instance When Replacing Tags

ID: Q229297


The information in this article applies to:


SYMPTOMS

When you use a For...Each loop to replace objects in a document in FrontPage Visual Basic for Applications (VBA), every other item is skipped in the loop.


CAUSE

The ALL collection is a dynamic collection. The collection loses a member each time a tag is replaced.

Essentially, the index value of a specific tag are changed dynamically.

When you use a For...Each loop to destroy tags, the tag with the first index number is replaced the first time the loop is run. The next tag is assigned the same index number as the one that was replaced. In the For...Each loop, the tag with that index number has already been looped through, and is skipped by the loop.


WORKAROUND

To work around this behavior, use a For loop instead of a For..Each loop.
The following example loops through anchor tags in a document, and replaces them with bold tags:


Sub testfor()
tagName = "a"
For i = 0 To ActiveDocument.all.tags(tagName).Length - 1
    Set tag = ActiveDocument.all.tags(tagName).Item(0)
    tag.outerHTML = "<b>" & tag.innerHTML & "</b>"
Next i
End Sub 


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

This problem occurs for all collections that you create with the Tags method, as well as the stock collections of the Document Object Model (such as the Anchor and Link collections of the FPHTMLDocument class).

However, this only occurs when elements are replaced inside of the For..Each loop.

If you need to access the elements for other purposes (such as setting attributes), the For..Each loop will work.

Additional query words: front page


Keywords          : kbdta 
Version           : WINDOWS:
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: July 1, 1999