title: Word VBA: Modifying words by looping ActiveDocument.Words creates an infinte loop
body: <p>I want to loop through a Word document word by word. I'm currently using the ActiveDocument.Words collection which seems pretty simple. But, there is a strange issue that if I change the content of one word, the internal pointer doesn't move to the next word but instead point's once more to the same word that just was modified. And in some cases this creates a situation where the routine get stuck in looping over a single word infinitely.</p>
  
  <p>An example code:
  </p>
  
  <pre><code>Sub LoopWords()
      Dim wd As Range
      i = 0
      For Each wd In ActiveDocument.Words
          If wd.Text = "foo " Then
              wd.Text = wd.Text &amp; "bar "
          End If
  
          'Prevent infinite loop:
          i = i + 1
          If i &gt; 99 Then
              Exit For
          End If
      Next wd
  End Sub
  </code></pre>
  
  <p>So for example using this macro in a doc containing a phrase "There is some foo in here." results in "There is some foo bar bar bar bar bar bar bar bar bar bar bar bar bar bar ..." and so on.</p>
  
  <p>So why on earth does it behave like this?<br/>
  How can I loop word by word and modify the text when necessary?</p>