c#Office Interop在循环浏览大文档中的每个单词时缓慢

时间:2018-06-04 19:58:11

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

我有以下代码段循环遍历Word文档中的每个单词,找到加粗的行(句子)并将它们存储在列表MasterSentences中。

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Document doc = new Document();
object path = fileDialog.FileName;
object missing = Type.Missing;

doc = word.Documents.Open(ref path, 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, ref missing);
string sentence = "";
foreach (Range rng in doc.StoryRanges)
{
    foreach (Range rngWord in rng.Words)
    {
        if ((rngWord.Text.Contains("\n") || rngWord.Text.Contains("\r")) && sentence != "")
        {
            MasterSentences.Add(sentence);
            sentence = "";
        }
        else if (rngWord.Bold != 0 && rngWord.Text != " " && rngWord.Text != "\t")
        {
            sentence += rngWord.Text;
        }
    }
}

问题是,对于包含23,742个单词的Word文档,这需要大约3-4分钟。

有没有办法提高速度?有没有更有效的方法来实现这一目标?

1 个答案:

答案 0 :(得分:0)

谢谢大家的反馈意见。似乎使用Range.Find.Execute是正确的方法。这是我更新的主要工作代码。现在只需要大约20秒,这是完美的。

Range rngFindBold = doc.Range();
rngFindBold.Find.Font.Bold = 1;
while (rngFindBold.Find.Execute(Format: true))
{
    if (!string.IsNullOrWhiteSpace(rngFindBold.Text))
    {
        MasterSentences.Add(rngFindBold.Text);
    }
}