FP2000: For Each Loop Skips Every Other Instance When Replacing Tags (229297)
The information in this article applies to:
This article was previously published under Q229297 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.
WORKAROUNDMicrosoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
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
STATUSMicrosoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. MORE INFORMATION
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.
Modification Type: | Minor | Last Reviewed: | 10/11/2006 |
---|
Keywords: | kbbug kbdta kbofficeprog kbpending kbProgramming KB229297 |
---|
|