3

我有一个内部 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 中弹出 SpellCheck 对话框并检测到任何拼写错误的单词,但我无法让它在 WinForm 应用程序中进行更正

此外,它使 Word Doc 的这个“外壳”打开,并带有更正的文本。我如何不显示或至少让它消失?

两件事情:

  • 首先,虽然“shell”关闭了它,但它每次都会闪烁。有什么解决办法吗?
  • 其次,拼写检查对话框没有出现在 TOP 上,我可以设置什么来纠正它?

谢谢

4

2 回答 2

1

接下来的步骤是:

  1. 将更正后的文本从文档中拉出。
  2. 关闭文档。(如果 Word 中只打开一个文档,您可能需要关闭或隐藏 Word 应用程序。)
  3. 将更正后的文本返回给调用函数。

更多信息:

于 2009-08-31T16:20:44.163 回答
0

我有一个旧脚本,其中所需的功能都被调用,因此单词本身不需要 DLL ref:

internal class SpellChecker
{
    public SpellChecker()
    {
    }

    public static string Check(string text)
    {
        bool flag;
        string str = text;
        flag = (text == null ? true : !(text != ""));
        bool flag1 = flag;
        if (!flag1)
        {
            Type typeFromProgID = Type.GetTypeFromProgID("Word.Application");
            object obj = Activator.CreateInstance(typeFromProgID);
            object[] objArray = new object[1];
            object obj1 = typeFromProgID.InvokeMember("Documents", BindingFlags.GetProperty, null, obj, null);
            object obj2 = obj1.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, obj1, null);
            object obj3 = obj2.GetType().InvokeMember("ActiveWindow", BindingFlags.GetProperty, null, obj2, null);
            objArray[0] = 0;
            obj3.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, null, obj3, objArray);
            object[] objArray1 = new object[] { -2000, -2000 };
            obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1);
            objArray[0] = "Spell Check";
            obj3.GetType().InvokeMember("Caption", BindingFlags.SetProperty, null, obj3, objArray);
            object obj4 = typeFromProgID.InvokeMember("Selection", BindingFlags.GetProperty, null, obj, null);
            objArray[0] = text;
            obj4.GetType().InvokeMember("TypeText", BindingFlags.InvokeMethod, null, obj4, objArray);
            objArray[0] = 6;
            obj4.GetType().InvokeMember("HomeKey", BindingFlags.InvokeMethod, null, obj4, objArray);
            object obj5 = obj2.GetType().InvokeMember("SpellingErrors", BindingFlags.GetProperty, null, obj2, null);
            int num = (int)obj5.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, obj5, null);
            flag1 = num <= 0;
            if (flag1)
            {
                System.Windows.Forms.MessageBox.Show("Spellcheck is correct");
            }
            else
            {
                obj3.GetType().InvokeMember("Activate", BindingFlags.InvokeMethod, null, obj3, null);
                objArray1 = new object[] { -5000, -5000 };
                obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1);
                objArray[0] = true;
                obj.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, obj, objArray);
                obj2.GetType().InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, obj2, null);
                objArray[0] = true;
                obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray);
                object obj6 = obj2.GetType().InvokeMember("Content", BindingFlags.GetProperty, null, obj2, null);
                str = obj6.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, obj6, null).ToString();
                str = str.Trim();
            }
            flag1 = obj == null;
            if (!flag1)
            {
                objArray[0] = true;
                obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray);
                obj.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, obj, null);
            }
        }
        string str1 = str;
        return str1;
    }
}

只需将文本输入它,它就会返回您批准的任何更正。

于 2015-12-02T09:17:20.303 回答