删除 MS Word 文档中的特定行
Deleting a specific line in an MS Word document
我一直在尝试从 Word 文档中删除特定行。逻辑是,如果我在文档中找到一个特定的词,我需要删除包含该词的特定行。到目前为止我只写了找到这个词的逻辑。但是,跟踪行号广告删除行,我无法做到。搜索了很多我的多个网站,但是,我现在很困惑。你能帮我解决这个问题吗?
下面是我的代码:-
void searchText(string txt)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\Users\SS5014874\Desktop\testdoc1.docx");
object missing = System.Reflection.Missing.Value;
doc.Content.Find.ClearFormatting();
object keyword = txt.ToString();
if (doc.Content.Find.Execute(ref keyword, 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))
{
//Need the logic to delete the line here
}
else
{
MessageBox.Show("Not found");
}
doc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);
}
如果您需要任何其他信息,请告诉我。
注意:搜索关键字由文本框给出,上面的函数是从按钮调用的。
您可以遍历文档的段落,然后在找到特定段落中的单词后,您可以删除该段落。
newDocument = wordApplication.Documents.Open(fileDoc,
confirmConversions: false,
addToRecentFiles: false,
readOnly: true,
passwordDocument: Password)
var docRange = newDocument .Content;
foreach(var para in docRange.Paragraphs)
{
if(para.ToString().Contains("word"))
{
docRange.Delete(para);
}
}
类似于
var range = doc.Content;
if (range.Find.Execute(txt))
{
range.Expand(WdUnits.wdLine); // or change to .wdSentence or .wdParagraph
range.Delete();
}
我一直在尝试从 Word 文档中删除特定行。逻辑是,如果我在文档中找到一个特定的词,我需要删除包含该词的特定行。到目前为止我只写了找到这个词的逻辑。但是,跟踪行号广告删除行,我无法做到。搜索了很多我的多个网站,但是,我现在很困惑。你能帮我解决这个问题吗?
下面是我的代码:-
void searchText(string txt)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open("C:\Users\SS5014874\Desktop\testdoc1.docx");
object missing = System.Reflection.Missing.Value;
doc.Content.Find.ClearFormatting();
object keyword = txt.ToString();
if (doc.Content.Find.Execute(ref keyword, 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))
{
//Need the logic to delete the line here
}
else
{
MessageBox.Show("Not found");
}
doc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);
}
如果您需要任何其他信息,请告诉我。
注意:搜索关键字由文本框给出,上面的函数是从按钮调用的。
您可以遍历文档的段落,然后在找到特定段落中的单词后,您可以删除该段落。
newDocument = wordApplication.Documents.Open(fileDoc,
confirmConversions: false,
addToRecentFiles: false,
readOnly: true,
passwordDocument: Password)
var docRange = newDocument .Content;
foreach(var para in docRange.Paragraphs)
{
if(para.ToString().Contains("word"))
{
docRange.Delete(para);
}
}
类似于
var range = doc.Content;
if (range.Find.Execute(txt))
{
range.Expand(WdUnits.wdLine); // or change to .wdSentence or .wdParagraph
range.Delete();
}