2016-07-01 5 views
0

私はWinFormsアプリケーションに単語のスペルチェックを統合しようとしています。これまでのところ、相互運用性は後部に深刻な痛みをもたらしました。何時間も騒がれた後、私は最終的に実際のスペルチェックを行いました。予想通りCheckSpellingOnceだけでなく、基礎となるCheckSpellingメソッドは実行しますが、できるだけ早く私はGetSpellingSuggestionsを呼び出すように、アプリケーションはスロー...Word.Interop document closed

例外をスロー:「System.Runtime.InteropServices.COMException」をClosedCaptionに。 Spelling.dll追加情報:開いているドキュメントがないため、このコマンドは利用できません。

最初のアイデアは、そのそれぞれのラッパーからの根本的なCOMオブジェクトが切断が_wordAppので、それが作成されたものとは異なるスレッドから呼び出されているということでした。だから私はCheckSpelling()内から呼び出すことを試みましたが、残念なことに同じ結果が出ました。また、ドキュメントの開閉を試み、既存のアプリケーションインスタンスに新しいドキュメントを追加したり、_documentオブジェクト(_document.Application.GetSpellingSuggestions)からアプリケーションを取得したりしています。

これは何ですか?

追加情報:CheckSpellingOnceメソッドは、タイマイベントが発生すると(ユーザーがRichTextFieldでタイピングを停止すると)UIを呼び出すようになります - 複数回 - 同じ_wordAppオブジェクトを使用して、起動を避けようとしていますwinword.exeの複数のインスタンス

/// <summary> 
    /// Checks spelling with the text from the provided richtextbox in a new thread. 
    /// </summary> 
    public void CheckSpellingOnce() 
    { 
     _checkerThread = new Thread(new ThreadStart(CheckSpelling)); 
     _checkerThread.Start(); 
    } 

    /// <summary> 
    /// Checks the spelling of a richtextbox. Raises an event with the result when done. 
    /// </summary> 
    private void CheckSpelling() 
    { 
     if (_shouldBeChecking) 
     { 
      RaiseStatusChanged(SpellCheckStatus.Working); 
      Word.ProofreadingErrors toReturn = null; 
      UpdateStringFromTextBox(); 

      if (!string.IsNullOrEmpty(_fromTextBox)) 
      { 
       _document.Content.Delete(); 
       _document.Words.First.InsertBefore(_fromTextBox); 

       _document.Content.LanguageID = _language; //Must be set specifically here for some f***d reason. 

       toReturn = _document.SpellingErrors; 

       RaiseSpellingChecked(toReturn); 
       RaiseStatusChanged(SpellCheckStatus.Idle); 
      } 
     } 
    } 

    public Word.SpellingSuggestions GetSpellingSuggestions(string word) 
    { 
     //throw new NotImplementedException(); 
     Word.SpellingSuggestions toReturn = _wordApp.GetSpellingSuggestions(word, _missing, _missing, _missing, _missing, _missing, _missing); 
     return toReturn; 
    } 

でもGetSpellingSuggestionsのこの実装で、それはそれ以上のもので、「toReturn」の行で不平を言い、そしてない...

 public void GetSpellingSuggestions(string word) 
    { 
     //throw new NotImplementedException(); 
     var _suggestionThread = new Thread(new ThreadStart(() => 
     { 
      _document.Content.Delete(); 
      _document.Words.First.InsertBefore(word); 

      _document.Content.LanguageID = _language; 
      Word.SpellingSuggestions toReturn = _wordApp.GetSpellingSuggestions(word, _missing, _missing, _missing, _missing, _missing, _missing); 
      Debug.Print(toReturn[0].ToString()); 
     })); 
     _suggestionThread.Start(); 
    } 
+0

あなたは[this](http://stackoverflow.com/questions/9718687/spell-checking-in-c-sharp-using-word-interop)SOの質問を見ましたか? –

+0

@JeroenHeier私は - 喜びはありません。 –

+0

エラーは_「開いている文書はありません」と表示されているため、スレッドの問題です。また、COMスレッドモデルは、複数のプロセスを扱うため、ここでは適用されません。 – MickyD

答えて

1
 /// <summary> 
     /// Opens a Word interop lib document. 
     /// </summary> 
     /// <returns></returns> 
     private Word._Document OpenDocument() 
     { 
      object visible = false; 

      _document = _wordApp.Documents.Add(_missing, _missing, _missing, visible); 
      return _document; 
     } 

これは私が私の文書を開いた方法だったが(インメモリ)。

可視パラメータを 'true'に設定すると問題が解決されますが、(私の場合のように)何らかの理由でアプリケーションプロセスがバックグラウンドで実行されます。 私の理論では、Document.CheckSpelling()のようなメソッドが呼び出されるまで、winword.exeは表示されず、実際にはWord GUIが呼び出されます。

誰かが必要な場合は、より多くのコードを提供できます。

おかげさまで、乾杯!

+0

誰が推測したでしょうか。あなたがそれを得てうれしい:) – MickyD