c# - 为什么向字符串添加换行符会在 Interop.Word 中产生错误?

标签 c# ms-word office-interop

此代码示例:

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 文档:

Prefix: text after the colon

但是,这段代码:

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

显然,有些东西是不正确的,但我无法准确指出是什么。

最佳答案

可能是错误的行结尾混淆了 Word - 尝试“冒号后的文本\r\n”“冒号后的文本”+Environment.NewLine。如果这不起作用,可能是因为换行符导致 Word 开始一个新段落,然后 para 指向它。由于新段落中没有文本,因此您无法选择范围。

如果是这种情况,那么为了达到相同的结果,您不应在文本中添加换行符:

  • 如果您想要第二段,请再次调用 report.Content.Paragraphs.Add
  • 您可以添加不是段落分隔符的换行符。这就是 Word 中的 Ctrl-Enter,并且应该可以用代码表达(但不知道如何实现)。

关于c# - 为什么向字符串添加换行符会在 Interop.Word 中产生错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11615343/

相关文章:

c# - 用语法糖强制算术优先级

vba - Word VBA 另存为错误 : incompatible file type and file extension

c# - Word Interop Delete 导致参数错误

c# - 与非线性 Excel 范围不同的列表 C#

vb.net - 如何通过Word Interop专门对齐每个段落?

c# - 我可以存储的 .NET 缓存 key 的限制是多少?

c# - 在 C# 中使用泛型时如何避免装箱/拆箱?

c# - 使用 Excel = Microsoft.Office.Interop.Excel 编译错误

c# - 为什么匿名类型没有索引器属性?

php - 各种文件类型、doc、dwg、图像的在线编辑器