使用隐藏在后面的MS Word 2010拼写检查功能

时间:2015-03-16 08:34:10

标签: c# .net winforms ms-word spell-checking

我在C#WinForms应用程序中使用MS Word的拼写检查功能。 这适用于Office 2007,但对于安装了Office 2010的计算机,我遇到了一些问题。应用程序一词在我当前的应用程序窗口后打开,应用程序似乎已冻结。使用alt + tab我可以使用单词app,但这不是用户友好的。

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

//int errors = 0;
if (p_TextToCheck != "")
{
    app.Visible = false;

    // Setting these variables is comparable to passing null to the function.
    // This is necessary because the C# null cannot be passed by reference.
    object template = Missing.Value;
    object newTemplate = Missing.Value;
    object documentType = Missing.Value;
    object visible = false;

    Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
    doc1.Words.First.InsertBefore(p_TextToCheck);
    //Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
    //errors = spellErrorsColl.Count;

    object optional = Missing.Value;

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

    //label1.Text = errors + " errors corrected ";
    object first = 0;
    object last = doc1.Characters.Count - 1;
    p_TextToCheck.Value = doc1.Range(ref first, ref last).Text;

    object saveChanges = false;
    object originalFormat = Missing.Value;
    object routeDocument = Missing.Value;
    doc1.Close(ref saveChanges, ref originalFormat, ref routeDocument);
}

object saveChanges1 = false;
object originalFormat1 = Missing.Value;
object routeDocument1 = Missing.Value;

app.Quit(ref saveChanges1, ref originalFormat1, ref routeDocument1);

1 个答案:

答案 0 :(得分:0)

我发现可靠的唯一方法(在多个Office版本上保持一致)避免保存对话框是将文档保存到临时位置,关闭Word,然后删除临时文件。