views:

307

answers:

3

I am using Word's Spell Check in my in house WinForm app. My clients are all XP machines with Office 2007 and randomly the spell check suggestion box pops up behind the App and makes everything "appear" frozen as you cannot get at it.

Suggestions? What do other people do to work around this or stop it altogether?

Thanks

Below is my code, for reference.

public class SpellCheckers
{
    public 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 = false;
        object optional = Missing.Value;
        object savechanges = false;
        app.ShowMe();

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

        Marshal.ReleaseComObject(doc);
        Marshal.ReleaseComObject(app);
        Marshal.ReleaseComObject(errors);

        return results;
    }
}

And I call it from my WinForm app like so -->

  public static void SpellCheckControl(Control control)
    {
        if (IsWord2007Available())
        {
            if (control.HasChildren)
            {
                foreach (Control ctrl in control.Controls)
                {
                    SpellCheckControl(ctrl);
                }
            }

            if (IsValidSpellCheckControl(control))
            {
                if (control.Text != String.Empty)
                {
                    control.BackColor = Color.FromArgb(180, 215, 195);
                    control.Text = Spelling.CheckSpelling(control.Text);
                    control.Text = control.Text.Replace("\r", "\r\n");
                    control.ResetBackColor();
                }
            }
        }
    }
A: 

It could be freezing up your application because the word document is running on the UI thread,try running your document in a new thread and use events to get it back to the UI thread

Tommy
It "freezes" because it is waiting for user input in the Spelling Suggestions Dialog but since it is hidden behind the App you can't get to it. Maybe that is what you are saying.....maybe.
Refracted Paladin
@Refracted Paladin: Then just mimimize your app or use a user32.dll function to bring your spell check window to the front
Tommy
@sniperX: Can't minimize the app as it is "locked". I will Google your `user32.dll` sugestion.
Refracted Paladin
@Refracted Paladin: mimimize it *before* it locks up
Tommy
+1  A: 

Did you try calling checkspelling with null instead of missing?

Here is my code. I used to have the same issue you were having but I was calling Checkspelling without any arguments. I use missing type just for adding the doucment..also note the WordDoc.Activate(); before checking spelling, I think that also helps push it to the front.

private object emptyItem = System.Reflection.Missing.Value;
private object oNothing = null;
private object oFalse = false;
    ...

    wordApp = New Word.Application();
    wordApp.Visible = False;


    WordDoc = WordApp.Documents.Add(ref emptyItem,ref emptyItem,ref emptyItem,ref oFalse);

                WordDoc.Words.First.InsertBefore(this.Text);

                Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors;
                SpellingErrors = docErrors.Count;
                WordDoc.Activate();
                WordApp.ShowWindowsInTaskbar = False;
                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);

luis
I will give this a try and report back. Thanks
Refracted Paladin
+1  A: 

I tried activating the window but it would bring up the entire word application and all I wanted was the spell check dialog to appear. I set the WordApp.WindowState right before calling CheckSpelling and that worked for me:

WordApp.WindowState = WdWindowState.wdWindowStateNormal; 
EmanP