2009-08-31 14 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 Docのこの「シェル」を修正したテキストで開いたままにします。それを表示しない、または少なくともそれを消さないにはどうすればいいですか?

2つのこと:

  • まず、 "シェル" は毎回それ 点滅を閉じても。 への任意の解決策は?
  • 第2に、スペルチェックダイアログで がTOPに表示されない場合は、 に設定するとどうなりますか?

おかげ

+0

その他の質問:これを静的にしない理由はありますか? –

+0

静的にしないと、特定のドキュメントやアプリケーションという言葉への参照を保持できます。それはあなたが新しいアプリやドキュメントの起動コストを避けるのに役立ちます。 (Visibleプロパティやアプリで遊んで、常に新しいアプリケーションを起動すると、文書を開いたままにしておくことができます) –

+0

私は 'それでは、Wordのシェルが開いたままになります。 –

答えて

1

次のステップは次のようになります。

  1. は、文書のうち、背面修正されたテキストを引き出します。
  2. 文書を閉じます。 Wordで1つの文書しか開いていない場合は、Wordアプリケーションを閉じたり隠したりすることができます。
  3. 修正したテキストを呼び出し関数に返します。

さらに詳しい情報:

+0

ありがとう、実際には "隠された"という言葉のテキストを修正して、それをつかむ必要があります。テキストを取り戻す方法についてのヒント? –

+0

選択オブジェクトを使用します。 「すべて選択」を行い、選択範囲からテキストを読み込みます。 –

+0

私のコメントを見るFULL答えの質問 –

0

私は必要な機能がすべての単語自体のために必要とされるので、何のDLL REFを呼び出されていない。このため、古いスクリプトを持っています:

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; 
    } 
} 

テキストを入力するだけで、あなたが承認した修正内容が返されます。

関連する問題