2012-02-16 45 views
1

我正在嘗試使用單詞來自動更正一些不是英文的文本問題是,當我使用SpellCheck函數「拼寫和語法」對話框彈出並等待用戶輸入我希望文本自動更正。所以我的問題是我如何解決這個問題?自動更正文字C#文字

using System.Collections.Generic; 
using Microsoft.Office.Interop.Word; 
using Word = Microsoft.Office.Interop.Word; 
using TobyCL.ro.toby.StringOperations; 
namespace namespace.ro.toby 
{ 
    class WordProofing:IProof 
    { 
     private readonly Word.Application _wordApp; 
     private readonly Word.Document _wordDoc; 
     private static object _oEndOfDoc = "\\endofdoc"; 
     public WordProofing() 
     { 

      _wordApp = new Word.Application {Visible = false}; 
      _wordDoc = _wordApp.Documents.Add(); 
     } 
     public void Close() 
     { 
      object obj = Word.WdSaveOptions.wdDoNotSaveChanges; 
      _wordDoc.Close(ref obj); 
      _wordApp.Quit(ref obj); 
     } 
     #region Implementation of IProof 

     public string Proof(string proofText) 
     { 
      Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range; 
      wRng.Text = proofText; 
      _wordDoc.CheckSpelling(IgnoreUppercase: true,AlwaysSuggest:false); 
      string str = wRng.Text; 
      wRng.Text = ""; 
      return str; 
     } 
     #endregion 
    } 
} 

我前幾天寫了這段代碼,它工作。問題是,我卸載校對工具來運行一些測試,現在我不斷得到該對話框,所以我想我可能必須設置一些Word設置,或者我已經改變了我的代碼中的東西,而不知道。任何幫助將不勝感激。

我使用Microsoft Office Word 2010中是否使用的是微軟Word版本

回答

2

對於誰可能感興趣,這是我設法解決它的方式,但它確實需要很長時間,所以任何改進或新想法都歡迎。

using Microsoft.Office.Interop.Word; 
    class WordProofing 
    { 
     private Application _wordApp; 
     private readonly Document _wordDoc; 
     private static object _oEndOfDoc = "\\endofdoc"; 
     public WordProofing() 
     { 

      _wordApp = new Application { Visible = false }; 
      _wordDoc = _wordApp.Documents.Add(); 
     } 
     public void Close() 
     { 
      _wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges); 
      _wordApp.Quit(); 
     } 

     public string Proof(string proofText) 
     { 
      Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range; 
      wRng.Text = proofText; 
      ProofreadingErrors spellingErros = wRng.SpellingErrors; 
      foreach (Range spellingError in spellingErros) 
      { 
       SpellingSuggestions spellingSuggestions = 
        _wordApp.GetSpellingSuggestions(spellingError.Text,IgnoreUppercase:true); 

       foreach (SpellingSuggestion spellingSuggestion in spellingSuggestions) 
       { 
        spellingError.Text = spellingSuggestion.Name; 
        break; 
       } 
      } 

      string str = wRng.Text; 
      wRng.Text = ""; 
      return str; 
     } 
    } 
0

默認情況下,拼寫檢查器會顯示對話框。要禁用對話框,我知道有兩種方法。

1)使用代碼,從自動修正中自動選擇第一個選項。

它是這樣的

AutoCorrect.Entries.Add Name:="AdSAD", Value:="Assad" 

2)或使用菜單選項。請參閱此鏈接。

主題自動拼寫正確與主詞典

話鏈接http://office.microsoft.com/en-us/word-help/automatically-correct-spelling-with-words-from-the-main-dictionary-HA010174790.aspx

不要讓我知道這是不是你想要的?

+0

菜單選項已經設置好了,而且我仍然不斷收到對話框,至於第一個選項,我不知道它是否有興趣使用word nit自己提供的建議。 – trebor 2012-02-16 10:52:08