WordEditor删除文档末尾的空白行

时间:2019-09-01 05:04:16

标签: c# ms-word office-interop word-addins

我正在尝试使用以下代码检测并删除文档特定范围内的空白行:

    Document doc = appointmentItem.GetInspector.WordEditor as Microsoft.Office.Interop.Word.Document;
    Bookmark bmkFound = doc.Bookmarks.get_Item("bmkToClean");
    Range bmkFound = bmkFound.Range;
    Find find = bmkFound.Find;
    find.Text = "\r";
    find.Replacement.Text = "";
    find.Execute();
    find.Text = "\r\n";
    find.Replacement.Text = "";
    find.Execute();
    find.Text = Convert.ToChar(13).ToString();
    find.Replacement.Text = "";
    find.Execute();
    find.Text = Convert.ToChar(10).ToString();
    find.Replacement.Text = "";
    find.Execute();

    lastChar.Text.Replace(Convert.ToChar(13), '');
    lastChar.Text.Replace(Convert.ToChar(10), '');
    lastChar.Text.Replace("\r", "");
    lastChar.Text.Replace("\n", "");

到目前为止,什么都没有解决。有提示吗?

1 个答案:

答案 0 :(得分:0)

好的,问题是我有几行空白,因此代码需要进行以下调整:

Document doc = appointmentItem.GetInspector.WordEditor as 
Microsoft.Office.Interop.Word.Document;
Bookmark bmkFound = doc.Bookmarks.get_Item("bmkToClean");
Range bmkFound = bmkFound.Range;
Find find = bmkFound.Find;
find.Text = "\r";
find.Replacement.Text = "";
find.Forward = true;
find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
find.Replacement.Text = "";
find.Execute(Replace: WdReplace.wdReplaceAll);