title: Using MS Word VBA how to Find and Replace highlighted text with the value of the highlight color
body: <p>Suppose the following text in an MS Word 2007/2010 document, I have "testA" highlighted with blue and "testB" highlighted with green color: "This is testA and testB.". I want to programmatically replace testA with its background color index 2, and replace testB with its background color index 11. Ref: <a href="https://msdn.microsoft.com/en-us/library/bb237561%28v=office.12%29.aspx" rel="nofollow">WdColorIndex Enumeration</a> </p>
  
  <p>I've tried the following but it has two issues:</p>
  
  <ol>
  <li>It replaces testA with 0 (the color index of default background) and testB with 2 (that is a color index of testA)</li>
  <li>While loop does not end</li>
  </ol>
  
  <p>I would like the replaced text to be: "This is 2 and 11". Instead, I am getting: "This is 0 and 2".</p>
  
  <p>Any corrections using VBA or C# will be ok.</p>
  
  <pre><code>Sub ReplaceHighlightedTextColor()
  
      With Selection.Find
          .ClearFormatting
          .Highlight = True
          While .Execute(Forward:=True, Format:=True, ReplaceWith:=CStr(Selection.FormattedText.HighlightColorIndex))
          Wend
      End With
  
  End Sub
  </code></pre>