我在试着在列表前插入文本。我有列表的Microsoft.Office.Interop.Word.Range,并在列表之前得到前面的范围。然后,我尝试将我的段落文本添加到前面的范围中,但是,该段是作为列表中的第一项而不是在列表之前添加的。
// rangeObj is the Range object for the list
var previousRange = rangeObj.Previous(MSWord.WdUnits.wdCharacter, 1);
Paragraph paragraph;
if (previousRange == null)
{
var rangeCopy = rangeObj.Duplicate;
rangeCopy.InsertParagraphBefore();
paragraph = rangeCopy.Paragraphs[1];
}
else
{
paragraph = previousRange.Paragraphs[1];
}
Range range = paragraph.Range;
range.Text = String.Format("{0} {1}", "My list", range.Text);例如在以下情况下:
(我想要的)之后:
我的名单
之后(我现在得到的):
编辑: Per Lasse的回答和我的评论,我得到了所有的工作,除了下面的边缘情况。注意:第二个列表不是第一个列表的子列表,每个列表之间没有空行。
if (previousRange.ListParagraphs.Count > 0)在此之前:
1. Item 3
2. Item 4
(我想要的)之后:
1. Item 3
2. Item 4
发布于 2012-11-10 11:24:29
在此代码中:
else
{
paragraph = previousRange.Paragraphs[1];
}。。您可能会在列表之前覆盖任何内容(包括只包含\r的空行),当我在一个示例上运行它时,很有道理的是,事情最终会被覆盖并发生奇怪的情况。
在此代码中:
if (previousRange == null)
{
var rangeCopy = rangeObj.Duplicate;
rangeCopy.InsertParagraphBefore();
paragraph = rangeCopy.Paragraphs[1];
}。。在列表前面插入一个新段落(这很好--尽管我不明白为什么需要克隆范围,如范围自动扩展到包括新插入的段落。),然后将新段落的范围存储在局部变量paragraph中。此时,paragraph = '\r'的内容-(如果您使用调试器执行应用程序,同时在调试阶段保持word可见,则可以看到这一点)。因此,此时光标就位于列表的前面,这就是您想要的位置--但是接下来执行以下操作:
Range range = paragraph.Range;
range.Text = "My paragraph";..。这意味着,您只需覆盖包括\r在内的所有内容,而不是对段落进行预挂文本,这将导致单词在列表中插入文本,而不是在其前面。
为了绕过这一问题,我提出了一种似乎可行的替代实现。它基于您使用列表前面的范围插入文本的想法。我已经为大多数行添加了评论,所以应该直接按照正在发生的事情进行:)
using System;
using System.Linq;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
namespace WordDocStats
{
internal class Program
{
private static void Main()
{
var wordApplication = new Application() { Visible = true };
// Open document A
var documentA = wordApplication.Documents.Open(@"C:\Users\MyUser\Documents\documentA.docx", Visible: true);
// This inserts text in front of each list found in the document
const string myText = "My Text Before The List";
foreach (List list in documentA.Lists)
{
// Range of the current list
var listRange = list.Range;
// Range of character before the list
var prevRange = listRange.Previous(WdUnits.wdCharacter);
// If null, the list might be located in the very beginning of the doc
if (prevRange == null)
{
// Insert new paragraph
listRange.InsertParagraphBefore();
// Insert the text
listRange.InsertBefore(myText);
}
else
{
if (prevRange.Text.Any())
{
// Dont't append the list text to any lines that might already be just before the list
// Instead, make sure the text gets its own line
prevRange.InsertBefore("\r\n" + myText);
}
else
{
// Insert the list text
prevRange.InsertBefore(myText);
}
}
}
// Save, quit, dones
Console.WriteLine("Dones");
Console.ReadLine();
documentA.Save();
wordApplication.Quit();
}
}
}简而言之,代码在给定文档中的每个列表之前插入一个字符串。如果在列表之前的行中已经有文本,则实现确保在列表之前和在列表之前的文本之后插入列表描述文本。
希望这会有所帮助:)
--更新以回答已编辑的问题:
在第二轮中,要完成你的要求,一种方法是做以下事情:
...
// The paragraph before the current list is also a list -> so in order to insert text, we must do "some magic"
else if(prevRange.ListParagraphs.Count > 0)
{
// First, insert a new line -> this causes a new item to be inserted into the above list
prevRange.InsertAfter("\r");
// Modify current range to be on the line of the new list item
prevRange.Start = prevRange.Start + 1;
prevRange.End = prevRange.Start;
// Convert the list item line into a paragraph by removing its numbers
prevRange.ListFormat.RemoveNumbers();
// Insert the text
prevRange.InsertBefore(myText);
}
...只需将该代码添加到我前面提供的示例代码的if-else循环内的foreach块中,您应该可以这样做:)我已经用MS 2013在我的机器上测试了它,它似乎还能工作。
https://stackoverflow.com/questions/13311310
复制相似问题