Word Interop逐字拼写文档太慢了

时间:2012-07-24 16:55:47

标签: interop ms-word spell-checking

使用下面的代码基本上我正在逐字检查文档并分开正确的文档。我工作,但太慢,我不得不等待超过10分钟4万字。当我打开相同的文档时,会在几秒钟内检查它。我做错了什么?

var application = new Application();
        application.Visible = false;
        Document document = application.Documents.Open("C:\\Users\\hrzafer\\Desktop\\spellcheck.docx");

        // Loop through all words in the document.
        int count = document.Words.Count;
        IList<string> corrects = new List<string>();
        for (int i = 1; i <= count; i++)
        {
            if (document.Words[i].SpellingErrors.Count == 0)
            {
                string text = document.Words[i].Text;
                corrects.Add(text);
            }
        }

        File.WriteAllLines("corrects.txt", corrects);
        application.Quit();

1 个答案:

答案 0 :(得分:0)

一种解决方法,因为您似乎只需要文档中的正确单词:

for i=ActiveDocument.Range.SpellingErrors.Count to 1 step -1
    ActiveDocument.Range.SpellingErrors(i).Delete
next

然后使用新名称保存文档

ActiveDocument.SaveAs FileName:="C:\MyWordDocs\NewFile.docx", _ 
    FileFormat:=wdFormatXMLDocument