Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q229297

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


Workaround

Microsoft 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
				

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


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.

↑ Back to the top


Keywords: KB229297, kbprogramming, kbpending, kbofficeprog, kbdta, kbbug

↑ Back to the top

Article Info
Article ID : 229297
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 258