title: MS Word VBA Find and Loop (NOT Replace)
body: <p>Sorry for the fundamental question but I am a LONGtime Access VBA developer who is getting my butt kicked by Word.</p>
  
  <p>I am building a solution for someone who uses a Legal Case Management System with a specific numbering scheme (aaa-aaa-##-##-###).  I need to search a document for hyphens and then grab the whole paragraph where a hyphen is found.  I then need to send the text from that paragraph to a separate function which parses out the file number from the paragraph (this function already works).</p>
  
  <p>If that function is successful, it returns the File Number, otherwise it returns "NOT FOUND".  So, I need to:</p>
  
  <ol>
  <li><p>Find all hyphens</p></li>
  <li><p>Capture the paragraph with a hyphen</p></li>
  <li><p>Pass that text to the function</p></li>
  <li><p>GO TO THE NEXT HYPHEN IF function returns "NOT FOUND"</p></li>
  </ol>
  
  <p>I have tried dozens of options without success.  I typically get stuck in an infinite loop (and do not seem to move forward) or I get to the end of the process with a false failure.</p>
  
  <p>I do not know how to move to the NEXT occurrence of a hyphen and repeat the process.  I also do not know how to run the process to the end of the document or stop at the end (without starting all over - because the hyphens REMAIN since this is NOT a replace process).</p>
  
  <p>I have tried so many different versions, but I included one below.</p>
  
  <p>Thanks for any guidance. I do appreciate it.</p>
  
  <p>DGP</p>
  
  <hr>
  
  <pre><code>Public Sub TestFind77()  '
      Selection.HomeKey Unit:=wdStory
  
      With Selection.Find
          .ClearFormatting
          .Text = "-"
          .Execute Forward:=True
  
          Do While .Found = True
              .Parent.Expand Unit:=wdParagraph
  
              Dim strWTF As String
              strWTF = .Parent
              'MsgBox strWTF
  
              strResult = fnGetFileNumberFromString(strWTF) ' This works
                  If strResult &lt;&gt; "NOT FOUND" Then
                  GoTo success
  
              End If
              .Execute Forward:=True
  
          Loop
  
      End With
  
    success:
      MsgBox strResult
  
  End Sub
  </code></pre>