2

此代码示例:

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

显然,有些东西是不正确的,但我不能准确地说明什么。

4

3 回答 3

1

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

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

  • 如果您想要第二段,请拨打另一个report.Content.Paragraphs.Add电话。
  • 您可以添加一个不是分段符的换行符。那是 Word 中的 Ctrl-Enter,并且应该可以在代码中表达(虽然不知道如何)。
于 2012-07-23T15:27:24.457 回答
1

猜想,它实际上并没有验证这一点,但行为是合乎逻辑的。\n 添加一个新段落,就像在 Word 中按 Enter 一样。因此,分配 Text 属性会更改段落引用,它现在指向添加的空段落。所以你的最终价值超出了范围。您必须参考上一段。或者只是不使用换行符。

于 2012-07-23T17:22:08.090 回答
1

我将添加一个答案,因为这可能对其他人有所帮助,而且我认为对已接受的答案发表评论太长了。

我在 Word 中的新行有一个相关问题:没有错误,但是有一个空格来开始新行。

例子:

string wordText = "First line";
wordText += "\r\n"; // or System.Environment.NewLine;
wordText += "Second line";

将在 Word 文档中给出以下结果:

First line
 Second line

看了这个问题后,我尝试只使用“\r”,它奏效了!所以现在我有:

string wordText = "First line";
wordText += "\r";
wordText += "Second line";

结果是:

First line
Second line
于 2013-04-18T15:59:34.727 回答