【问题标题】:Implement Word Spell check in Windows Form App在 Windows 窗体应用程序中实现单词拼写检查
【发布时间】: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


【解决方案1】:

接下来的步骤是:

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

更多信息:

【讨论】:

  • 谢谢,所以它实际上更正了“隐藏”单词文档中的文本,然后我需要抓住它。关于我如何将文本提取出来的任何提示?
  • 使用选择对象。执行“全选”,然后从所选内容中读出文本。
  • 查看我对问题的评论以获得完整答案
【解决方案2】:

我有一个旧脚本,其中所需的功能都被调用,因此单词本身不需要 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;
    }
}

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

【讨论】:

    猜你喜欢
    • 2014-07-13
    • 1970-01-01
    • 2017-06-22
    • 2011-02-13
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2011-01-23
    • 2019-09-19
    相关资源
    最近更新 更多