【问题标题】:WordEditor remove blank lines at end of documentWordEditor 删除文档末尾的空行
【发布时间】:2019-09-01 05:04:16
【问题描述】:

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

    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", "");

到目前为止,没有任何结果。有什么提示吗?

【问题讨论】:

  • 您是否尝试过逐行调试代码?
  • 是的。没有错误但没有替换:-/
  • 可以从代码示例中删除带有 \r 和 \n 的行 - 这些行只会找到该文字。我希望 13 和 10 能够工作,就 finding 而言。如果您在末尾注释掉替换内容,并添加 find.Select(); 是最后选择的 13 还是 10?如果是,则将替换更改为:find.Replacement.Text = ""; 并将其放在 之前 Execute。请注意,这可能仍不能完全满足您的需要,但应确认如何查找/替换段落标记/行尾字符。
  • 您好辛迪,感谢您的回答。 C# 中不存在 find.Select() 函数。
  • 根据问题中的代码findRange,所以find.Select(); 必须工作。 Find.Select(); 但不会。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2021-09-27
    相关资源
    最近更新 更多