从Word Interop文档中删除文本

问题描述:

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

问题是它工作不正确,不能提供预期的结果。我不能理解接口在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(); 
} 
+0

做'如何从文本中移除谷歌搜索word文档C#Microsoft.Interop'看看这个一些可能的指针以及http://stackoverflow.com/questions/10231132/how-to-remove-text-from-word-document-footer-using-c-sharp – MethodMan

+1

如果你使用'.docx'文件而不是'.doc'文件,我建议你停止使用interop类并切换到[微软发布的新SDK](https://msdn.microsoft.com/en-us /library/office/bb448854.aspx)专门用于处理'docx'文件。 –

+0

是的,我做了很多谷歌搜索,并查看了其他问题/答案。其他我没有找到与我的问题有关的答案,或者我错过了一些东西。在大多数情况下,使用Interop创建文档并没有太大麻烦。出于某种原因,我听起来很简单。 – Bobby

给你想办法的缺点是开始和结束位置(从的开始字符数文档故事)将根据不可见/非打印字符的不同而不同。内容控制,字段代码和其他内容会影响这一点 - 所有这些都会以不同的方式进行,具体取决于查询的方式。

更可靠的是将起始点存储在一个Range中,然后将其扩展到终点。

我还建议使用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(); 
    } 
} 
+0

谢谢!这解决了我的问题。我必须忽略Range.Find方法,这非常有效。我确实需要将'rngFind.Execute'改为'rngFind.Find.Execute',但就是这样。 – Bobby

+0

糟糕,关于“懒惰的手指” - 我已经更正了我的答案中的代码。很高兴帮助:-) –

这是完全未经测试,并提供了一个建议:

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; 
    } 
}