title: Word 2010 VBA Replace within a highlighted range
body: <p>The following code works, but it performs everything on the <em>entire document</em>. I'd like to highlight a block of text, then when I run the macro <em>only</em> have it work on the highlighted text. How do I do that? Thanks...</p>
  
  <pre><code>Sub DoCodeNumberStyle(numchars As String)
  
      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      With Selection.Find
          .Text = "(^13)([0-9]{" + numchars + "}) "
          .Replacement.Text = "\1###\2$$$ "
          .Forward = True
          .Wrap = wdFindAsk
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchAllWordForms = False
          .MatchSoundsLike = False
          .MatchWildcards = True
      End With
      Selection.Find.Execute Replace:=wdReplaceAll
      Selection.Find.ClearFormatting
      Selection.Find.Replacement.ClearFormatting
      Selection.Find.Replacement.Style = ActiveDocument.Styles("CodeNumber")
      With Selection.Find
          .Text = "###([0-9]{" + numchars + "})$$$"
          .Replacement.Text = "\1"
          .Forward = True
          .Wrap = wdFindAsk
          .Format = True
          .MatchCase = False
          .MatchWholeWord = False
          .MatchAllWordForms = False
          .MatchSoundsLike = False
          .MatchWildcards = True
      End With
      Selection.Find.Execute Replace:=wdReplaceAll
  End Sub
  
  Sub CodeNumberStyle()
      DoCodeNumberStyle ("1")
      DoCodeNumberStyle ("2")
  End Sub
  </code></pre>
  
  <p><strong>PostScript:</strong></p>
  
  <p>One additional thing I've discovered: if you do more than one find on a Selection, the first find loses/changes the Selection, so the others are no longer bounded by the original Selection (and a wdReplaceAll will continue to the end of the document). To fix this, capture the Selection into a Range. Here's the final version of my method, which now does everything I need, is restricted to the original highlighted selection (even with 3 find-and-replacements), and has also been minimized, code-wise:</p>
  
  <pre><code>Sub AAACodeNumberStyleHighlightedSelection()
  
      With Selection.Range.Find
          .ClearFormatting
          .Style = ActiveDocument.Styles("Code")
          .Replacement.ClearFormatting
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchAllWordForms = False
          .MatchSoundsLike = False
  
          ' First line:
          .Text = "1   //"
          .Replacement.Text = "###1$$$   //"
          .MatchWildcards = False
          .Execute Replace:=wdReplaceAll
  
          ' Rest of lines:
          .Text = "(^13)([0-9]{1,2}) "
          .Replacement.Text = "\1###\2$$$ "
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
  
          ' Now style the line numbers:
          .Text = "###([0-9]{1,2})$$$"
          .Replacement.Text = "\1"
          .Replacement.Style = ActiveDocument.Styles("CodeNumber")
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
  
      End With
  
  End Sub
  </code></pre>