Search code examples
c#ms-wordoffice-interop

Why does adding a newline to a string create an error in Interop.Word?


This code sample:

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);

successfully creates a Word document with the following text:

Prefix: text after the colon

However, this 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"; // <-- 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);

throws a COMException: Value out of range exception on this line:

Range bold = report.Range(ref start, ref end);

The only difference between these two code snippets is the new line character in this line:

String suffix = "text after the colon\n"; // <-- Note the additional newline in the string

Stack trace

I don't know if it's helpful, but this is the full stack trace of the error:

at Microsoft.Office.Interop.Word.DocumentClass.Range(Object& Start, Object& 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()

Debugging results

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:

para.Range.Text == "Prefix: text after the colon\r"
start == 0
end == 0

For the second result, these are the values:

para.Range.Text == "\r"
start == 28
end == 34

Clearly, something is incorrect, but I can't place exactly what.


Solution

  • Could be wrong line endings confusing Word - try "text after the colon\r\n" or "text after the colon" + Environment.NewLine. If that doesn't work, it might be because the newline character causes Word to start a new paragraph, and then para points to it. Since the new paragraph has no text in it, you can't select a range.

    If that's the case, then to achieve the same result, you shouldn't put newlines in the text:

    • If you want a second paragraph, make another report.Content.Paragraphs.Add call.
    • You could add a line break that's not a paragraph break. That's Ctrl-Enter in Word, and should be expressible in code (don't know how though).