c# - 使用 Word Interop 删除特定的换行符

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

我在使用 word interop 删除一些特定的换行符时遇到了问题。似乎 doc.Content.Text 的索引与 word.. 的索引不匹配。

我需要删除所有没有点作为前一个字符的分隔符。

对于小文档,它可以工作,但之后只有大约 3-5 个中断,所选范围不是分页符,任何人都知道如何在没有范围的情况下删除这些中断。选择(开始:开始,结束:结束)或者知道问题所在?

例如我有以下文字:

美国调查人员调查了 340,000 人,发现周一的情绪并不比其他工作日差,周五除外。((break))

人们在临近周末时更加快乐,支持((中断删除))的概念

“那个星期五的感觉”。((休息))

class Program
{
    static void Main(string[] args)
    {
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        word.Visible = true;

        string document = @"C:\bin\Debug\123.doc";

        if (document != null)
        {
            Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);
            doc.Activate();
            string text = doc.Content.Text;

            int index = text.IndexOf("\r");
            int deletetCount = 0;

            while(index != -1)
            {
                if (index != 0 && text[index - 1] != '.')
                {
                    int start = index + 1 - deletetCount;
                    int end = start + 1;
                    if (start >= 0 && end >= 0 && end > start)
                    {
                        Range range = doc.Range(Start: start, End: end);
                        range.Select();
                        range.Delete();
                        deletetCount++;
                    }
                }

                index = text.IndexOf("\r", index + 1);
            }
        }
    }
}

最佳答案

我找到了一个解决方案:

现在我使用搜索和替换通配符(正则表达式)

删除中断的规则:

  • 前一个字符不是点或冒号
  • 文本不是粗体(对于我来说,粗体文本主要是标题)
  • 不应删除带有以下文本的中断(列表):a。 b. C。 1. 2. 3. --> 要匹配列表,必须将它们转换为文本 (doc.ConvertNumbersToText())

.

  class Program
  {
    static void Main(string[] args)
    {
        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        word.Visible = true;

        string document = @"C:\bin\Debug\1.doc";

        if (document != null)
        {
            Document doc = word.Documents.Open(document, ReadOnly: false, Visible: true);
            doc.Activate();

            object missingObject = null;

            doc.ConvertNumbersToText();

            object item = WdGoToItem.wdGoToPage;
            object whichItem = WdGoToDirection.wdGoToFirst;
            object replaceAll = WdReplace.wdReplaceAll;
            object forward = true;
            object matchWholeWord = false;
            object matchWildcards = true;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object wrap = WdFindWrap.wdFindAsk;
            object format = true;
            object matchCase = false;
            object originalText = "([!.:])^13(?[!.])";
            object replaceText = @"\1 \2";

            doc.GoTo(ref item, ref whichItem, ref missingObject, ref missingObject);
            foreach (Range rng in doc.StoryRanges)
            {
                rng.Find.Font.Bold = 0;

                rng.Find.Execute(ref originalText, ref matchCase,
            ref matchWholeWord, ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms, ref forward,
            ref wrap, ref format, ref replaceText, ref replaceAll, ref missingObject,
            ref missingObject, ref missingObject, ref missingObject);
            }

        }
    }
}

关于c# - 使用 Word Interop 删除特定的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12039923/

相关文章:

vba - 字 VBA : Get Range between Consecutive Headings

c++ - 对打开的文档执行 Word 自动化是否安全?

excel - vba从word表复制到excel

javascript - Office 365 加载项 - 如何在加载项 Windows 之间进行通信

javascript - 如何在 Windows Phone 8.1 应用程序中在 C# 和 Javascript 之间传递数据或通信

c# - 验证 WCF 和 WebService 方法参数值的最佳方式

c# - 解析文本框中的日期时间

ms-office - O365 同意流程中的身份验证和权限错误

Java办公组件

c# - 如何更改控制台应用程序上的目录