title: Word.Range : Move Range index in the formatted text that corresponds to the plain text
body: <p><strong>I need to analyze text of my Word document, and create bookmarks on range of text my analyzer has detected (<em>almost like a grammar checker</em>).</strong></p>
  
  <p>I don't want use <code>Find()</code> utility, because my needs are too specific.</p>
  
  <hr>
  
  <h1>Explanations</h1>
  
  <p>For that,</p>
  
  <p><strong>1/ Retrieve Document plain text</strong> </p>
  
  <p>I Retrieve Plain text of the main story of my document :</p>
  
  <blockquote>
    <p>String plainText = ActiveDocument.Range().Text;</p>
  </blockquote>
  
  <p><strong>2/ Analyze plain text and get results</strong> </p>
  
  <p>I send it to my analyzer tool which return a collection of marker with position :
  For example, if I wanted to detected the pattern "<em>my pattern</em>" in the document text, analyzer could return a marker as <code>{ pattern : "my marker", start: 5, end : 14 }</code>, where "<em>start</em>" and "<em>end</em>" are the character indexes of the pattern in the  plain text sent.</p>
  
  <p><strong>3/ Display results in Document</strong></p>
  
  <p>I create bookmark from theses markers
  For previously example, it woold be :</p>
  
  <pre><code>// init a new range and collapse it 
  Word.Range range = activeDocument.Range(); range.Collapse(WdCollapseStart); 
  
  // move character-by-character in the "formatted" text
  range.MoveStart(WdUnits.Character, Marker.start ); # Marker.start=5
  
  //set length (end)
  range.setRange(range.Start,range.Start+(Marker.End-Marker.Start)); #Marker.end=14
  </code></pre>
  
  <p><strong>4/ Results</strong></p>
  
  <p><strong>4.1 Global Result</strong></p>
  
  <p>Everything is OK when Document Main Story Contains Text, links, lists, titles : 
  Ranges are well positionned, Plain Text indexes correlate with formatted text indexes.</p>
  
  <p><strong>4.2 Arrays Issue</strong></p>
  
  <p>When a document contains an array, Ranges are bad positionned a few characters : Plain Text indexes correlate not exactly with formatted text indexes.</p>
  
  <p>I found the reason of this issue (It was explained in others forums) : this  is due to non printing char(7), which is a cell delimiter added in plain text. We can handle these chars to calculate position range and everything is OK !</p>
  
  <p><strong>4.3 Issue for Content Controls, Table of contents, Sections and others</strong></p>
  
  <p>When a document contains theses elements, Ranges are also bad positionned a few characters.
  Others non printing appears in plain text but I don't understand what it means and how deal with to calculate position range.</p>
  
  <p>By displaying Word element markers with "Developer ribbon > creation mode", we see 2 markers per elements : shifting plain text indexes by 2*elements resolve issues. It's seems OK.</p>
  
  <p><strong>4.4 Issue with Endpaper</strong></p>
  
  <p>I don't know how we says "page de garde" (french) in english, I think it's "endpaper" : this is the first page with specific header, footer and content controls :)</p>
  
  <p>When a document contains an Endpaper, Ranges are also bad positionned a few characters.
  But this time, there are not non printing marker in the plain text. </p>
  
  <p>Other info, when I display word element markers with "Developer ribbon > creation mode", I see endpaper markers.</p>
  
  <hr>
  
  <h1>Questions</h1>
  
  <ul>
  <li>How detect Endpaper in Word Document Range ?</li>
  <li>How understand Plain Text indexes don't always correlate with formatted text indexes, in function of Word document elements which contains ?</li>
  <li>XML nodes manipulation would be a more reliable alternative for that? If yes, could you give me good examples to manage bookmars or others in current document with XML Api ?</li>
  </ul>
  
  <hr>
  
  <h1>Others ressources</h1>
  
  <p>I found similar issues :</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3772938/correlate-range-text-to-range-start-and-range-end">Correlate Range.Text to Range.Start and Range.End</a></li>
  <li><a href="http://www.vbaexpress.com/forum/showthread.php?36710-Strange-character-on-table-range-text" rel="nofollow noreferrer">http://www.vbaexpress.com/forum/showthread.php?36710-Strange-character-on-table-range-text</a></li>
  </ul>
  
  <p><strong>I hope my explanations are clear and you can help me to understand what is wrong or show me a best way to do that ?</strong></p>
  
  <p>Thanks, really.</p>