开发者

Word Spell Check pops up hidden and "freezes" my App

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 arou开发者_运维知识库nd 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();
                }
            }
        }
    }


I know this is from 2010, but it's taken all day to figure out that the spell checker pop-up is dependent on two classes (before you write all your logic).

This is that definitions of your Word Application and your Word document.

Word.Application app = new Word.Application();

needs to be:

Word._Application app = new Word.Application();

and the document (which is correct in the original question) needs to be

Word._Document doc = app.Documents.Add([Missing.Value passes]);

If you use the none underscored classes you will receive no errors, but either the spellchecker will be "trapped" inside the hidden word app, locking up your app whilst it awaits user input, or it will pop up behind your app, a result which is just as bad if you're working with a full screen locked application - as we are with our in-house software.

Hope this helped!


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; 


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


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


On top of CheckSpelling() Method I have added following block of code which helped me for VS2010 winform application

    [DllImport("user32.dll")]  
    private static extern IntPtr GetForegroundWindow();

    public string CheckSpelling(string text)
    {
     ---------
     ****Your code for Spell Check ****
     ---------
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜