FP2000: For Each Loop Skips Every Other Instance When Replacing TagsID: Q229297
|
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.
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.
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
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.
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).Additional query words: front page
Keywords : kbdta
Version : WINDOWS:
Platform : WINDOWS
Issue type : kbbug
Last Reviewed: July 1, 1999