此代码示例:
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
显然,有些事情是不正确的,但我无法准确地说出什么。
答案 0 :(得分:1)
可能是错误的行结尾令人困惑的Word - 尝试"text after the colon\r\n"
或"text after the colon" + Environment.NewLine
。如果这不起作用,可能是因为换行符导致Word开始一个新段落,然后para
指向它。由于新段落中没有文字,因此您无法选择范围。
如果是这种情况,那么为了达到相同的结果,你不应该在文本中添加换行符:
report.Content.Paragraphs.Add
来电。答案 1 :(得分:1)
猜想,它实际上没有验证这一点,但行为是合乎逻辑的。 \ n添加一个新段落,就像按Enter键一样。因此,分配Text属性会更改段落引用,它现在指向添加的空段落。因此, end 值超出范围。你必须参考前一段。或者只是不使用换行。
答案 2 :(得分: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