C# Spell checker Problem

Posted by reggie on Stack Overflow See other posts from Stack Overflow or by reggie
Published on 2010-05-04T17:10:38Z Indexed on 2010/05/04 17:18 UTC
Read the original article Hit count: 380

Filed under:
|
|
|
|

I've incorporated spell check into my win forms C# project. This is my code.

public void CheckSpelling()
{
    try
    {
        // declare local variables to track error count 
        // and information
        int SpellingErrors = 0;
        string ErrorCountMessage = string.Empty;

        // create an instance of a word application
        Microsoft.Office.Interop.Word.Application WordApp =
            new Microsoft.Office.Interop.Word.Application();

        // hide the MS Word document during the spellcheck
        //WordApp.WindowState = WdWindowState.wdWindowStateMinimize;


        // check for zero length content in text area
        if (this.Text.Length > 0)
        {
            WordApp.Visible = false;

            // create an instance of a word document
            _Document WordDoc = WordApp.Documents.Add(ref emptyItem,
                                              ref emptyItem,
                                              ref emptyItem,
                                              ref oFalse);

            // load the content written into the word doc
            WordDoc.Words.First.InsertBefore(this.Text);

            // collect errors form new temporary document set to contain
            // the content of this control
            Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors;
            SpellingErrors = docErrors.Count;

            // execute spell check; assumes no custom dictionaries
            WordDoc.CheckSpelling(ref oNothing, ref oIgnoreUpperCase, ref oAlwaysSuggest,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing, ref oNothing,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing);

            // format a string to contain a report of the errors detected
            ErrorCountMessage = "Spell check complete; errors detected: " + SpellingErrors;

            // return corrected text to control's text area
            object first = 0;
            object last = WordDoc.Characters.Count - 1;
            this.Text = WordDoc.Range(ref first, ref last).Text;
        }
        else
        {
            // if nothing was typed into the control, abort and inform user
            ErrorCountMessage = "Unable to spell check an empty text box.";
        }

        WordApp.Quit(ref oFalse, ref emptyItem, ref emptyItem);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);

        // return report on errors corrected
        // - could either display from the control or change this to 
        // - return a string which the caller could use as desired.
       // MessageBox.Show(ErrorCountMessage, "Finished Spelling Check");
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

The spell checker works well, the only problem is when I try to move the spell checker the main form blurs up for some reason. Also when I close the spell checker the main form is back to normal. It seems like it is opening up Microsoft word then hiding the window, only allowing the spell checker to be seen. Please help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about c#3.0