2012-07-24 30 views
0

下面的代碼基本上是我逐字檢查一個文檔並分開正確的文檔。我工作,但速度太慢,我必須等待超過10分鐘的4萬字。當我打開相同的文件時,幾秒鐘後會檢查它。我究竟做錯了什麼?Word Interop按字拼寫檢查文檔太慢

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

回答

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