在 Windows 窗体应用程序中实现单词拼写检查

发布于 2024-08-04 00:33:02 字数 1979 浏览 7 评论 0原文

我有一个内部 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 文档“外壳”。我如何不显示这一点或至少让它消失?

两件事:

  • 首先,虽然“外壳”将其关闭 每次都闪一下任何解决方案 那?
  • 其次,拼写检查对话框可以 没有出现在TOP上,我可以设置什么 更正吗?

谢谢

I have an In-house windows form app that I would like to use Spell Checking in. Everyone has Office 2007 installed so I shouldn't have an issue there but I am having trouble getting this to fully work.

Here is what I have:

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;
    }
}

}

I use this like so:

memDirectionsToAddress.Text = SpellCheckers.CheckSpelling(memDirectionsToAddress.Text);

Now this successfully pops up the SpellCheck Dialog from Word and detects any misspelled wordsbut I cannot get it to make the corrections in the WinForm app.

Also, it leaves this "Shell" of a Word Doc open with the corrected text. How do I not show that or at least make it go away?

Two things:

  • First, though the "shell" closes it
    Flashes everytime. Any solutions to
    that?
  • Second, the Spell Check Dialog does
    not appear on TOP, what can I set to
    correct that?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后来的我们 2024-08-11 00:33:02

接下来的步骤是:

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

更多信息:

The next steps would be:

  1. Pull the corrected text back out of the document.
  2. Close the document. (If there's only one document open in Word, you may want to close or hide the Word application.)
  3. Return the corrected text to the calling function.

More info:

千鲤 2024-08-11 00:33:02

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

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;
    }
}

只需向其提供文本,它将返回您批准的任何更正。

I have an old script for this where the required features are all invoked so no DLL ref are required for word itself:

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;
    }
}

Just feed it the text and it will return with any corrections you approve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文