3

我在我的内部 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();
                }
            }
        }
    }
4

5 回答 5

4

我知道这是从 2010 年开始的,但是花了一整天的时间才发现拼写检查器弹出窗口依赖于两个类(在你编写所有逻辑之前)。

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

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

需要是:

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

并且文件(在原始问题中是正确的)需要是

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

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

希望这有帮助!

于 2013-04-10T13:58:44.973 回答
2

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

WordApp.WindowState = WdWindowState.wdWindowStateNormal; 
于 2010-08-18T21:39:45.293 回答
1

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

于 2010-03-04T20:38:01.037 回答
1

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

这是我的代码。我曾经遇到过同样的问题,但我在没有任何参数的情况下调用 Checkspelling。我使用缺少的类型只是为了添加文档。还要注意 WordDoc.Activate(); 在检查拼写之前,我认为这也有助于将其推到前面。

私有对象 emptyItem = System.Reflection.Missing.Value;
私有对象 oNothing = null;
私有对象 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);

于 2010-03-24T00:11:21.477 回答
0

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

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

    public string CheckSpelling(string text)
    {
     ---------
     ****Your code for Spell Check ****
     ---------
    }
于 2017-12-08T07:05:05.010 回答