Word Spell Check pops up hidden and freezes my application

I use Word Spell Check in my home WinForm application. My clients are all XP computers with Office 2007, and at random the spellcheck prompt window appears behind the application and causes everything to “appear” frozen, since you cannot get it.

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

thank

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 this from my WinForm application, for example: →

  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();
                }
            }
        }
    }
+3
source share
5 answers

, 2010 , , ( ).

Word Application Word.

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

:

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

( )

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

, , " " , , , , , , - .

, !

+4

, -, , , - . WordApp.WindowState CheckSpelling, :

WordApp.WindowState = WdWindowState.wdWindowStateNormal; 
+2

, , , .

+1

checkpelling , ?

. , , Checkspelling - . doucment. WordDoc.Activate(); , , .

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

+1
source

In addition to the CheckSpelling () method, I added the following code block that helped me in the VS2010 winform application

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

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

Source: https://habr.com/ru/post/1735485/


All Articles