单词拼写检查会弹出隐藏并“冻结”我的应用程序

发布于 2024-08-24 15:50:15 字数 2348 浏览 5 评论 0原文

我在我的内部 WinForm 应用程序中使用 Word 的拼写检查。我的客户都是装有 Office 2007 的 XP 机器,应用程序后面会随机弹出拼写检查建议框,使所有内容“看起来”冻结,因为您无法访问它。

建议?其他人会采取什么措施来解决这个问题或完全阻止它?

谢谢

以下是我的代码,供参考。

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

我从我的 WinForm 应用程序中调用它,就像这样 -->

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦冥 2024-08-31 15:50:15

我知道这是 2010 年的事情,但花了一整天的时间才弄清楚拼写检查弹出窗口依赖于两个类(在编写所有逻辑之前)。

这就是您的 Word 应用程序和 Word 文档的定义。

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

需要是:

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

和文档(在原始问题)需要是

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

如果您使用无下划线的类,您将不会收到错误,但拼写检查器会收到错误将被“困”在隐藏的单词应用程序内,在等待用户输入时锁定您的应用程序,或者它将在您的应用程序后面弹出,如果您正在使用全屏锁定的应用程序,结果同样糟糕 - 就像我们使用我们的内部软件。

希望这有帮助!

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!

陌伤ぢ 2024-08-31 15:50:15

我尝试激活该窗口,但它会调出整个单词应用程序,而我想要的只是出现拼写检查对话框。我在调用 CheckSpelling 之前设置了 WordApp.WindowState,这对我有用:

WordApp.WindowState = WdWindowState.wdWindowStateNormal; 

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; 
戏蝶舞 2024-08-31 15:50:15

它可能会冻结您的应用程序,因为 Word 文档正在 UI 线程上运行,请尝试在新线程中运行您的文档并使用事件将其返回到 UI 线程

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

稳稳的幸福 2024-08-31 15:50:15

您是否尝试使用 null 而不是 Missing 来调用拼写检查?

这是我的代码。我曾经遇到过与您相同的问题,但我在没有任何参数的情况下调用拼写检查。我使用缺失的类型只是为了添加文档。还要注意 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);

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

自在安然 2024-08-31 15:50:15

在 CheckSpelling() 方法之上,我添加了以下代码块,这对我的 VS2010 winform 应用程序有帮助

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

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

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 ****
     ---------
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文