Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8266923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:20:12+00:00 2026-06-08T05:20:12+00:00

This code sample: object miss = System.Reflection.Missing.Value; object Visible = true; Microsoft.Office.Interop.Word.Application WordApp =

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:20:14+00:00Added an answer on June 8, 2026 at 5:20 am

    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).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is a code sample from Microsoft(MSDN) (build sql dependency application) , could you
this is the simple code: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)
Why doesn't object.__init__ take *args, **kwargs as arguments? This breaks some simple code in
I'm reading this code sample : And since I don't know C#, I decided
For one or another reason this code sample gives 48 hours instead of 24,
In this particular code sample I want to reference the second overloaded method (int
I am new to scheme. This is code sample from SICP course of MIT.
I'm trying to learn the Enterprise Library. I found this useful code sample to
In this sample code the URL of the app seems to be determined by
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.