VBA: ForEach vs. For-Loop RRS feed

  • Question

  • Hi!

    I have a Word document with 2 paragraph with each 1 footnote.
    I select the first paragraph with one footnote. When i iterate over the footnotes, i will get both footnotes with the ForEach . But when i use the second for-loop. I get only one footnote.

    What´s the difference with the loops? When i use C# and Interop, it´s the same behavior.

    Thank you!!!

    'Application.ActiveDocument.Paragraphs(1).Range.Footnotes.Count is 1!!!
    For Each f In Application.ActiveDocument.Paragraphs(1).Range.Footnotes
        MsgBox f.Index
        ' the loop runs twice; for each footnote in document
    For i = 1 To Application.ActiveDocument.Paragraphs(1).Range.Footnotes.Count
        MsgBox Application.ActiveDocument.Paragraphs(1).Range.Footnotes(i).Index
        ' the loop runs one time; for the one footnote in the paragraph

    Tuesday, March 16, 2021 9:15 PM

All replies

  • The for loop executes a statement or a block of statements repeatedly until a specified expression evaluates to false. 
    There is need to specify the loop bounds (minimum or maximum). Following is a code example of a simple for loop that starts 0 till <= 5. 
    1. int j = 0;  
    2. for (int i = 1; i <= 5; i++)  
    3. {  
    4.    j = j + i ;  
    5. }  
    The foreach statement repeats a group of embedded statements for each element in an array or an object collection. You do not need to specify the loop bounds minimum or maximum. The following code loops through all items of an array.
    1. int j = 0;  
    2. int[] myArr = new int[] { 0, 1, 2, 3, 5, 8, 13 };  
    3. foreach (int i in myArr )  
    4. {  
    5.    j = j + i ;  
    6. }  
    foreach: Treats everything as a collection and reduces the performance. foreach creates an instance of an enumerator (returned from GetEnumerator()) and that enumerator also keeps state throughout the course of the foreach loop. It then repeatedly calls for the Next() object on the enumerator and runs your code for each object it returns.
    • Using for loop we can iterate a collection in both direction, that is from index 0 to 9 and from 9 to 0. But using for-each loop, the iteration is possible in forward direction only.
    • In variable declaration, foreach has five variable declarations (three Int32 integers and two arrays of Int32) while for has only three (two Int32 integers and one Int32 array).
    • When it goes to loop through, foreach copies the current array to a new one for the operation. While for doesn't care of that part.

    The other advantage of foreach is that it works on any IEnumerable, where as for only makes sense for IList, where each element actually has an index.

    However, if you need to use the index of an element, then of course you should be allowed to use a for loop. But if you don't need to use an index, having one is just cluttering your code.

    1 : For Loops executes a block of code until an expression returns false.

    1 : ForEach loop executed a block of code through the items in object collections.

    2 : For loop can execute with object collections or without any object collections.

    2 : ForEach loop can execute with object collections only.

    Wednesday, March 17, 2021 8:58 AM
  • In addition to Pranam's response, it looks like Microsoft has good documentation on examples of For...Next loops (For loops) and For Each...Next loops (For Each loops). I find that For Each loops are nice if one wants to process members of an array once. Whereas, For loops can be used for the previous case AND some powerful processing that involves iterating over members multiple times.

    For your case, have you performed any debugging to make sure that Application.ActiveDocument.Paragraphs(1).Range.Footnotes.Count contains the expected output?

    For...Next Statements

    For Each...Next Statements

    • Edited by Zac Gab Wednesday, October 6, 2021 4:50 PM
    Wednesday, October 6, 2021 4:41 PM