从 Word Interop 文档中删除文本

deleting text from a Word Interop document

我在尝试使用 Word Interop 从 Word 文档中删除 data/text 列表时遇到问题。到目前为止,我认为我可以通读文档以找到起始文本,然后找到结束文本,并将这些索引中的每一个保存到它们自己的变量中。接下来,我将循环遍历从起始索引到结束索引的数据,并删除其间的所有文本。

问题是它工作不正常,没有提供预期的结果。我一定不理解 Range 界面在 document.Paragraphs[i+1].Range.Delete(); 中是如何工作的。它删除了一些但不是全部的行,并且似乎超出了我想要删除的段落。我错过了什么?必须有更好的方法来做到这一点。 Interop 的文档似乎很少。

string text = " "; 
int StartLocation = 0;
int EndLocation = 0;
//I roughly know the starting location
//starting at I=2248 so I don't
//through entire document                             
for (int i = 2248; i < 2700; i++)
{
  text = document.Paragraphs[i + 1].Range.Text.ToString();
  if (text.Contains("firstWordImSearchingFor"))
  {
     StartLocation = i;
  }
  if (text.Contains("lastWordImSearchingFor"))
  {
     EndLocation = i;                        
  }
}
//delete everything between those paragraph locations 
//(not working correctly/ skips lines)
for(int i = StartLocation; i<EndLocation-1i++)
{
   document.Paragraphs[i+1].Range.Delete(); 
}

这完全未经测试,仅作为建议提供:

var docRange = document.Content;
bool inDelete = false;
foreach(var para in docRange.Paragraphs)
{
    if(para.ToString().Contains("Start flag") || inDelete)
    {
        inDelete = true;
        docRange.Delete(para);
    }
    if (para.ToString().Contains("End flag"))
    {
        // remove following line to retain this paragraph
        docRange.Delete(para);
        break;
    }
}

您尝试的方法的缺点是开始和结束位置(文档故事开头的字符数)将根据存在的不可见/非打印字符而有所不同。内容控件、字段代码和其他因素会影响这一点——所有方式都不同,具体取决于查询方式。

更可靠的方法是将起点存储在一个范围内,然后将其扩展到终点。

我也推荐使用Range.Find搜索起点和终点。

简单的伪代码示例,因为我没有足够的信息继续为您提供完整的工作代码:

Word.Range rngToDelete = null;
Word.Range rngFind = document.Content;
bool wasFound = false;
object missing = System.Type.Missing;
object oEnd = Word.WdCollapseDirection.wdCollapseEnd;
wasFound = rngFind.Find.Execute("firstWordImSearchingFor", ref missing, ref missing, 
           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (wasFound)
{
  rngToDelete = rngFind.Duplicate //rngFind is now where the term was found!
  //reset the range to Find so it moves forward
  rngFind.Collapse(ref oEnd);
  rngFind.End = Document.Content.End 
      wasFound = rngFind.Find.Execute("lastWordImSearchingFor", ref missing, ref missing, 
           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  if (wasFound)
  {
    rngToDelete.End = rngFind.End;
    rngToDelete.Delete();
  }
}