此代码示例:
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);
成功创建了一个包含以下文本的 Word 文档:
前缀:冒号后的文本
但是,这段代码:
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);
COMException: Value out of range
在此行引发异常:
Range bold = report.Range(ref start, ref end);
这两个代码片段之间的唯一区别是这一行中的换行符:
String suffix = "text after the colon\n"; // <-- Note the additional newline in the string
堆栈跟踪
我不知道它是否有帮助,但这是错误的完整堆栈跟踪:
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()
调试结果
再一次,它可能没有帮助,但调试每个代码示例都会产生这些结果。对于第一个结果,这些是引发异常的行之前的变量值:
para.Range.Text == "Prefix: text after the colon\r"
start == 0
end == 0
对于第二个结果,这些是值:
para.Range.Text == "\r"
start == 28
end == 34
显然,有些东西是不正确的,但我不能准确地说明什么。