I'm trying to integrate word spell checking into a WinForms application. So far, the interop lib has been a severe pain in the rear. After many hours of messing around with it, I finally got the actual spell checking to work. The CheckSpellingOnce as well as the underlying CheckSpelling methods perform as expected, but as soon as I call GetSpellingSuggestions, the application throws a...
Exception thrown: 'System.Runtime.InteropServices.COMException' in ClosedCaption.Spelling.dll Additional information: This command is not available because no document is open.
First idea was that the underlying COM object disconnects from its' respective wrapper because _wordApp is being called from a different thread than it was created. So I tried calling it from within CheckSpelling(), unfortunately with the same results. I've also tried opening and closing the document, adding a new document to the existing application instance, as well as getting the application from the _document object itself (_document.Application.GetSpellingSuggestions).
So what gives?
Additional info: the CheckSpellingOnce method gets called from the UI when a timer event gets fired (once the user stops typing in the RichTextField) - so multiple times - using the same _wordApp object, as I am trying to avoid launching multiple instances of 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;
}
Even with this implementation of GetSpellingSuggestions, it complains at the "toReturn" line, and not at the ones above it...
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();
}