title: Why does adding a newline to a string create an error in Interop.Word?
body: <p>This code sample:</p>
  
  <pre><code>object miss = System.Reflection.Missing.Value;
  object Visible = true;
  Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
  Document report = WordApp.Documents.Add(ref miss, ref miss, ref miss, ref miss);
  
  Paragraph para = report.Content.Paragraphs.Add(ref miss);
  String prefix = "Prefix";
  String suffix = "text after the colon";
  String allText = prefix + ":" + suffix;
  para.Range.Text = allText;
  para.Range.Font.Bold = 0;
  para.Range.Font.Size = 12;
  
  Object start = para.Range.Start;
  Object end = para.Range.Start + allText.IndexOf(":");
  
  Range bold = report.Range(ref start, ref end);
  bold.Bold = 1;
  
  para.Range.InsertParagraphAfter();
  
  object filename = "test.doc";
  report.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
  object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
  object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
  object routeDocument = true;
  ((_Document)report).Close(saveChanges, originalFormat, routeDocument);
  </code></pre>
  
  <p>successfully creates a Word document with the following text:</p>
  
  <blockquote>
    <p><strong>Prefix</strong>: text after the colon</p>
  </blockquote>
  
  <p>However, this code:</p>
  
  <pre><code>object miss = System.Reflection.Missing.Value;
  object Visible = true;
  Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
  Document report = WordApp.Documents.Add(ref miss, ref miss, ref miss, ref miss);
  
  Paragraph para = report.Content.Paragraphs.Add(ref miss);
  String prefix = "Prefix";
  String suffix = "text after the colon\n"; // &lt;-- Note the additional newline in the string
  String allText = prefix + ":" + suffix;
  para.Range.Text = allText;
  para.Range.Font.Bold = 0;
  para.Range.Font.Size = 12;
  
  Object start = para.Range.Start;
  Object end = para.Range.Start + allText.IndexOf(":");
  
  Range bold = report.Range(ref start, ref end);
  bold.Bold = 1;
  
  para.Range.InsertParagraphAfter();
  
  object filename = "test.doc";
  report.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
  object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
  object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
  object routeDocument = true;
  ((_Document)report).Close(saveChanges, originalFormat, routeDocument);
  </code></pre>
  
  <p>throws a <code>COMException: Value out of range</code> exception on this line:</p>
  
  <pre><code>Range bold = report.Range(ref start, ref end);
  </code></pre>
  
  <p>The only difference between these two code snippets is the new line character in this line:</p>
  
  <pre><code>String suffix = "text after the colon\n"; // &lt;-- Note the additional newline in the string
  </code></pre>
  
  <h3>Stack trace</h3>
  
  <p>I don't know if it's helpful, but this is the full stack trace of the error:</p>
  
  <pre><code>at Microsoft.Office.Interop.Word.DocumentClass.Range(Object&amp; Start, Object&amp; End)
  at Sandbox.Program.Main(String[] args) in C:\Users\ricardo\Documents\Visual Studio 2010\Projects\Sandbox\Sandbox\Program.cs:line 48
  at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <h3>Debugging results</h3>
  
  <p>Once again, it may not be helpful, but debugging each code sample yields these results. For the first result, these are the values of the variables immediately before the line that raises the exception:</p>
  
  <pre><code>para.Range.Text == "Prefix: text after the colon\r"
  start == 0
  end == 0
  </code></pre>
  
  <p>For the second result, these are the values:</p>
  
  <pre><code>para.Range.Text == "\r"
  start == 28
  end == 34
  </code></pre>
  
  <p>Clearly, something is incorrect, but I can't place exactly what. </p>