title: VBA for each loop locked when I revert revisions
body: <p>I want to get both the revised and original text from a document. I do it this way:</p>
  <pre><code>Set wrdDoc = wrdApp.Documents.Open(fileName)
  
  For each sent in wrdDoc.Sentences
  
      if sent.Revisions.Count &gt;=0 then
          after=sent.text
          sent.Revisions.RejectAll 
          before=sent.text
          SaveRev(before,after)
      End if
  
  next
  </code></pre>
  <p>Now that would be fine, except that malformed sentences like</p>
  <blockquote>
  <p>This is one sentence.This is another.</p>
  </blockquote>
  <p>Will get parsed in a weird way. First, there will be this one: &quot;<strong>This is one sentence.</strong>&quot;, then this one with both &quot;<strong>This is one sentence.This is another.</strong>&quot;</p>
  <p>What happens when there are revisions there? The first iteration will revert revisions on the first sentence, then the second iteration will not 'see' that revised portion.</p>
  <p>Bottom line is, the first iteration will get both versions of the first sentence, and the second iteration will get only the original version of the first sentence (while getting both versions from the second sentence).</p>
  <p>Let me clarify:</p>
  <p>Let's say I had the original</p>
  <blockquote>
  <p>We started with this sentence.And this sentence.</p>
  </blockquote>
  <p>And it was revised to</p>
  <blockquote>
  <p>We ended with this sentence.And this other sentence.</p>
  </blockquote>
  <p>First iteration will result in</p>
  <blockquote>
  <p>Before: We started with this sentence.</p>
  <p>After: We ended with this sentence.</p>
  </blockquote>
  <p>But second iteration will have</p>
  <blockquote>
  <p>Before: We ended with this sentence.And this sentence.</p>
  <p>After: We ended with this sentence.And this other sentence.</p>
  </blockquote>
  <p>Well, what I did was alter the logic, undoing the revision reversion:</p>
  <pre><code>Set wrdDoc = wrdApp.Documents.Open(fileName)
  
  For each sent in wrdDoc.Sentences
  
      if sent.Revisions.Count &gt;=0 then
          wrdDoc.Undo
          after=sent.text
          sent.Revisions.RejectAll 
          before=sent.text
          SaveRev(before,after)
      End if
  
  next
  </code></pre>
  <p>I like this because I end up with an unaltered document (except for the last sentence).</p>
  <p>The thing is, doing this puts the macro in an infinite loop at one specific sentence.</p>
  <p>I have no idea of the mechanics of the for each, I have no clue what is causing it to hang. Obviously altering the collection is messing up the loop, but I don't understand why.</p>
  <p>I could loop for i=0 to wrdDoc.Sentences.Count, but I think that will make me skip sentences for the same reasons I'm repeating one now, and I cannot risk it (even if I test OK, I have to be sure it will never happen).</p>
  <p>So the question is (are):</p>
  <ol>
  <li>Can any one help me figuring out why it's locking on a sentence,</li>
  <li>Is there a better way of doing this?</li>
  <li>How can I solve it while making sure not to skip sentences.</li>
  </ol>
  <p>Thank you very much!</p>
  <p>PS: I can provide sample documents, let me know if it's needed (maybe what I'm doing wrong is already clear to someone, and I'd have to make the samples as I cannot share the documents I'm working on).</p>
  <p>--EDIT--</p>
  <p>Ok so this is where it's hanging, only on the 32nd file.</p>
  <p>It doesn't hang on a sentence, it actually does a few at the start of the document, then goes back to the beginning.</p>
  <p>I previously encountered the same error, but it looped in a single sentence, and didn't go back to the beginning. I think it's the same issue. I'll try to reproduce original and revised versions here.</p>
  <p>Originalversion</p>
  <pre><code>MAIN TITLE
  
  Measurement of some variable
  
  1   REQUIRED TOOLS
  
  1.1 Special tools
  
  NOTe:
  
  Some note about the procedure (unaltered by revision)
  
  Equipment name (carrier returned line)
  (english) assemply with Equipment PN
  Kit
  
  Equipment name (carrier returned line)
  (english) assemply with (Another) Equipment PN
  Kit
  
  Document continues...
  </code></pre>
  <p>There are 2 equipment entries before it restarts the loop.</p>
  <p>Revision consisted of inserting the document number, some First Letter of the Word caps, and changing the order between Equipment PN and &quot;Kit&quot;.</p>
  <p>Revised version</p>
  <pre><code>ducument number
  MAIN TITLE
  
  Measurement of Some Variable
  
  1   REQUIRED TOOLS
  
  1.1 Special Tools
  
  NOTe:
  
  Some note about the procedure (unaltered by revision)
  
  Equipment name (carrier returned line)
  (english) assemply with kit
  Equipment PN
  
  
  Equipment name (carrier returned line)
  (english) assemply with kit
  (Another) Equipment PN
  
  
  Document continues...
  </code></pre>
  <p>Recorded original/revison pairs were:</p>
  <p>Original..................................Revised</p>
  <p>{Empty}...................................Document number</p>
  <p>Measurement of some variable..............Measurement of Some Variable</p>
  <p>Special tools............................Special Tools</p>
  <p>(english) assemply with..................(english) assemply with kit</p>
  <p>(english) assemply with..................(english) assemply with kit</p>
  <p>Then it starts again, recording the same entries until I break.
  I don't see the sentences overlapping I talked about, but there was a line break insertion on the revision.</p>
  <p>Thanks!</p>