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

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

我知道这是从2010年开始的,但需要一整天才能确定拼写检查器弹出窗口依赖于两个类(在编写所有逻辑之前)。

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

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

需要是:

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

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

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

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

希望这有帮助!

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

 WordApp.WindowState = WdWindowState.wdWindowStateNormal; 

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

您是否尝试使用null调用checkpelling而不是丢失?

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

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

在CheckSpelling()方法的基础上,我添加了以下代码块,这有助于我获得VS2010 winform应用程序

  [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); public string CheckSpelling(string text) { --------- ****Your code for Spell Check **** --------- }