【发布时间】:2015-12-02 17:17:20
【问题描述】:
我有一个内部 Windows 窗体应用程序,我想使用拼写检查。每个人都安装了 Office 2007,所以我应该没有问题,但我无法让它完全工作。
这是我所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
namespace Refraction.Spelling
{
public static class SpellCheckers
{
public static 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 = true;
object optional = Missing.Value;
object savechanges = false;
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);
return results;
}
}
}
我是这样使用的:
memDirectionsToAddress.Text = SpellCheckers.CheckSpelling(memDirectionsToAddress.Text);
现在这成功地从 Word 中弹出拼写检查对话框并检测到任何拼写错误的单词但我无法让它在 WinForm 应用程序中进行更正。
此外,它会打开 Word Doc 的这个“外壳”并显示更正的文本。我如何不显示或至少让它消失?
两件事:
- 首先,虽然“shell”关闭了它 每次都闪烁。任何解决方案 那个?
- 其次,拼写检查对话框 没有出现在TOP,我可以设置什么 正确吗?
谢谢
【问题讨论】:
-
附加问题:有什么理由不做这个静态的吗?
-
如果您不将其设为静态,则可以保留对特定文档和/或单词应用程序的引用。这将帮助您避免新应用程序或文档的启动成本。 (您可以通过使用 Visible 属性或应用程序并始终启动新应用程序来保持文档打开但不可见。)
-
问题是,如果我最后不做
app.Close(),那么 Word 的外壳就会保持打开状态...... -
我不得不更改
object visible = true;并添加app.ShowMe();下面的答案帮助指导了我,这就是我接受它的原因。
标签: c# .net winforms office-2007 spell-checking