2009-08-31 77 views
3

我有一個內部Windows窗體應用程序,我想使用拼寫檢查。每個人都安裝了Office 2007,所以我不應該有一個問題,但我遇到問題這完全可以工作。在Windows窗體應用程序中執行Word拼寫檢查

以下是我有:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Word = Microsoft.Office.Interop.Word; 
using System.Reflection; 

namespace Refraction.Spelling 
{ 
    public static class SpellCheckers 
    { 
     public static string CheckSpelling(string text) 
     { 
      Word.Application app = new Word.Application(); 
object nullobj = Missing.Value; 
       object template = Missing.Value; 
       object newTemplate = Missing.Value; 
       object documentType = Missing.Value; 
       object visible = true; 
       object optional = Missing.Value; 
      object savechanges = false; 
      Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible); 

     doc.Words.First.InsertBefore(text); 
     Word.ProofreadingErrors errors = doc.SpellingErrors; 

     var ecount = errors.Count; 
     doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional, 
      ref optional, ref optional, ref optional, ref optional, ref optional, 
      ref optional, ref optional, ref optional); 
     object first = 0; 
     object last = doc.Characters.Count - 1; 
     var results = doc.Range(ref first, ref last).Text; 
     doc.Close(ref savechanges, ref nullobj, ref nullobj); 
     app.Quit(ref savechanges, ref nullobj, ref nullobj); 

     return results; 
    } 
} 

} 

我用這個像這樣:

memDirectionsToAddress.Text = SpellCheckers.CheckSpelling(memDirectionsToAddress.Text); 

現在,這成功地從Word中彈出的拼寫檢查對話框,並檢測到任何拼寫錯誤的單詞 ,但我不能讓它會在WinForm應用程序 中進行更正。

此外,它使Word文檔的這個「殼」打開並顯示正確的文本。我怎麼不顯示或至少讓它消失?

兩件事情:

  • 首先,儘管 「殼」 關閉它 閃爍每次。任何解決方案 ?
  • 其次,拼寫檢查對話框確實沒有出現在頂部 ,我能設置爲 正確嗎?

感謝

+0

附加問題:是否有任何理由不使這個靜態? – 2009-08-31 16:30:45

+0

如果您沒有將其設爲靜態,則可以保留對特定文檔和/或單詞應用程序的引用。這將幫助您避免新應用程序或文檔的啓動成本。 (您可以通過使用Visible屬性或應用程序並始終啓動一個新應用程序來保持文檔的打開狀態但不可見。) – 2009-08-31 17:46:43

+0

問題是,如果我沒有執行'app.Close()'結束然後Word的殼保持打開.... – 2009-08-31 18:11:30

回答

1

下一個步驟將是:

  1. 修正文本拉回了文件。
  2. 關閉文檔。 (如果在Word中僅打開一個文檔,則可能需要關閉或隱藏Word應用程序。)
  3. 將更正的文本返回給調用函數。

更多信息:

+0

謝謝,所以它實際上糾正了「隱藏」word文檔中的文本,然後我需要抓住它。有關我如何將文本取出的提示? – 2009-08-31 16:30:04

+0

使用選擇對象。做一個「全部選擇」,然後從選擇中讀取文本。 – 2009-08-31 16:36:05

+0

請參閱我對問題的評論以獲得完整答案 – 2009-09-10 19:05:43

0

我對這個舊腳本,所需的功能都被調用所以沒有DLL裁判所必需的詞本身:

internal class SpellChecker 
{ 
    public SpellChecker() 
    { 
    } 

    public static string Check(string text) 
    { 
     bool flag; 
     string str = text; 
     flag = (text == null ? true : !(text != "")); 
     bool flag1 = flag; 
     if (!flag1) 
     { 
      Type typeFromProgID = Type.GetTypeFromProgID("Word.Application"); 
      object obj = Activator.CreateInstance(typeFromProgID); 
      object[] objArray = new object[1]; 
      object obj1 = typeFromProgID.InvokeMember("Documents", BindingFlags.GetProperty, null, obj, null); 
      object obj2 = obj1.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, obj1, null); 
      object obj3 = obj2.GetType().InvokeMember("ActiveWindow", BindingFlags.GetProperty, null, obj2, null); 
      objArray[0] = 0; 
      obj3.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, null, obj3, objArray); 
      object[] objArray1 = new object[] { -2000, -2000 }; 
      obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1); 
      objArray[0] = "Spell Check"; 
      obj3.GetType().InvokeMember("Caption", BindingFlags.SetProperty, null, obj3, objArray); 
      object obj4 = typeFromProgID.InvokeMember("Selection", BindingFlags.GetProperty, null, obj, null); 
      objArray[0] = text; 
      obj4.GetType().InvokeMember("TypeText", BindingFlags.InvokeMethod, null, obj4, objArray); 
      objArray[0] = 6; 
      obj4.GetType().InvokeMember("HomeKey", BindingFlags.InvokeMethod, null, obj4, objArray); 
      object obj5 = obj2.GetType().InvokeMember("SpellingErrors", BindingFlags.GetProperty, null, obj2, null); 
      int num = (int)obj5.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, obj5, null); 
      flag1 = num <= 0; 
      if (flag1) 
      { 
       System.Windows.Forms.MessageBox.Show("Spellcheck is correct"); 
      } 
      else 
      { 
       obj3.GetType().InvokeMember("Activate", BindingFlags.InvokeMethod, null, obj3, null); 
       objArray1 = new object[] { -5000, -5000 }; 
       obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1); 
       objArray[0] = true; 
       obj.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, obj, objArray); 
       obj2.GetType().InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, obj2, null); 
       objArray[0] = true; 
       obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray); 
       object obj6 = obj2.GetType().InvokeMember("Content", BindingFlags.GetProperty, null, obj2, null); 
       str = obj6.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, obj6, null).ToString(); 
       str = str.Trim(); 
      } 
      flag1 = obj == null; 
      if (!flag1) 
      { 
       objArray[0] = true; 
       obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray); 
       obj.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, obj, null); 
      } 
     } 
     string str1 = str; 
     return str1; 
    } 
} 

只需提供文本,它將返回您批准的任何更正。