2010-05-04 129 views
1

我已經將拼寫檢查合併到了我的win窗體C#項目中。這是我的代碼。C#拼寫檢查問題

public void CheckSpelling() 
{ 
    try 
    { 
     // declare local variables to track error count 
     // and information 
     int SpellingErrors = 0; 
     string ErrorCountMessage = string.Empty; 

     // create an instance of a word application 
     Microsoft.Office.Interop.Word.Application WordApp = 
      new Microsoft.Office.Interop.Word.Application(); 

     // hide the MS Word document during the spellcheck 
     //WordApp.WindowState = WdWindowState.wdWindowStateMinimize; 


     // check for zero length content in text area 
     if (this.Text.Length > 0) 
     { 
      WordApp.Visible = false; 

      // create an instance of a word document 
      _Document WordDoc = WordApp.Documents.Add(ref emptyItem, 
               ref emptyItem, 
               ref emptyItem, 
               ref oFalse); 

      // load the content written into the word doc 
      WordDoc.Words.First.InsertBefore(this.Text); 

      // collect errors form new temporary document set to contain 
      // the content of this control 
      Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors; 
      SpellingErrors = docErrors.Count; 

      // execute spell check; assumes no custom dictionaries 
      WordDoc.CheckSpelling(ref oNothing, ref oIgnoreUpperCase, ref oAlwaysSuggest, 
       ref oNothing, ref oNothing, ref oNothing, ref oNothing, ref oNothing, 
       ref oNothing, ref oNothing, ref oNothing, ref oNothing); 

      // format a string to contain a report of the errors detected 
      ErrorCountMessage = "Spell check complete; errors detected: " + SpellingErrors; 

      // return corrected text to control's text area 
      object first = 0; 
      object last = WordDoc.Characters.Count - 1; 
      this.Text = WordDoc.Range(ref first, ref last).Text; 
     } 
     else 
     { 
      // if nothing was typed into the control, abort and inform user 
      ErrorCountMessage = "Unable to spell check an empty text box."; 
     } 

     WordApp.Quit(ref oFalse, ref emptyItem, ref emptyItem); 
     System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp); 

     // return report on errors corrected 
     // - could either display from the control or change this to 
     // - return a string which the caller could use as desired. 
     // MessageBox.Show(ErrorCountMessage, "Finished Spelling Check"); 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show(e.ToString()); 
    } 
} 

拼寫檢查效果很好,唯一的問題是,當我嘗試檢查的主要形式因爲某些原因模糊了移動的咒語。另外,當我關閉拼寫檢查器時,主窗體恢復正常。它似乎是打開微軟的話,然後隱藏窗口,只允許看到拼寫檢查器。請幫忙。

回答

1

我試過使用你的示例代碼,它不能正常工作,所以我嘗試了MSDN's tutorial on the subject

這就是說,我覺得這是一個相當黑客的解決方案。至於你的主要表格模糊起來,我想這是因爲它停止響應,而你在拼寫檢查窗口?您可以通過使用新線程來解決它。

另外,你是對的,它啓動MS Word,然後隱藏窗口。

個人而言,我寧願使用像NetSpell這樣的庫,而不是依靠Office。

+0

我也在尋找一個WinForms拼寫檢查器。我很好奇:你還在使用NetSpell嗎?首先,答案中的鏈接指向一篇近10年的文章(2003年)。此外,圖像只顯示一個對話框。這是否意味着它無法在RichTextBox控件中顯示這些紅色波浪線?謝謝。 – 2011-04-20 16:48:32

0

我的工作和測試代碼片段如下:

string s1 = textBox1.Text; 

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); 

Microsoft.Office.Interop.Word._Document doc = app.Documents.Add(); 

doc.Words.First.InsertBefore(s1); 

Microsoft.Office.Interop.Word.ProofreadingErrors errors = doc.SpellingErrors; 

int errorCount = errors.Count; 

doc.CheckSpelling(Missing.Value, true, false); 

app.Quit(false); 

textBox3.Text = errorCount.ToString(); 

與錯誤的文本應用。

Application with wrong text

的Windows顯示錯誤的字爲紅色突出顯示的文本。

Word plugin checking word spells

錯誤總數被顯示在最後。

Application showing total number of errors

的解決方案是從我blog拍攝。