使用 Word Interop 在 C# 中进行拼写检查 [英] Spell Checking in C# Using Word Interop

查看:33
本文介绍了使用 Word Interop 在 C# 中进行拼写检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 word.dll(Word Interop API)在 C# 中编写拼写检查应用程序.

我想检查哪些拼写不正确,并相应地获得不正确单词的建议.

我从网上得到了一个示例代码,但我无法理解以下命令的参数:

Microsoft.Office.Interop.Word._Application.GetSpellingSuggestions(字符串,引用对象,引用对象,引用对象,引用对象,引用对象,引用对象,引用对象,引用对象,引用对象,引用对象,引用对象,ref 对象,ref 对象)

我只想知道所有 ref 对象 的含义是什么?我想知道它们的含义.

解决方案

我前几天和这个一起工作,想分享我的发现,并在已经给出的答案中添加一点.

你问:

<块引用>

我想检查哪些拼写不正确并相应地得到错误词的建议.

(...)

我只想知道所有引用对象"的含义是什么?我想了解它们的含义.

对此的简短回答是 - 查看 文档.

为了向您展示如何在更完整的上下文中使用 GetSpellingSuggestions 方法,我在下面包含了一个示例程序.请注意,您可以使用 language 变量更改所需的校对语言.代码如下:

使用系统;使用 Microsoft.Office.Interop.Word;命名空间 WordStack{公开课计划{私有静态无效 Main(){//创建一个新的 Word 应用程序实例(并保持不可见)var wordApplication = new Application() { Visible = false };//必须加载一个文档var myDocument = wordApplication.Documents.Open(@"C:...myDoc.docx");//设置语言var language = wordApplication.Languages[WdLanguageID.wdEnglishUS];//设置自定义字典的文件名// -  基于://http://support.microsoft.com/kb/292108//http://www.delphigroups.info/2/c2/261707.htmlconst string custDict = "custom.dic";//获取拼写建议var 建议 = wordApplication.GetSpellingSuggestions("overfloww", custDict, MainDictionary: language.Name);//将每个建议打印到控制台foreach(SpellingSuggestion 建议中的 SpellingSuggestion)Console.WriteLine("建议替换:{0}", spellingSuggestion.Name);Console.ReadLine();wordApplication.Quit();}}}

...这给了我以下三个建议:溢出溢出溢出.

给定的示例是使用 .NET 4.5 和 Word Interop API (Office 2013) 15 版实现的.

请注意,给定的示例还解决了您对已经给出的答案之一的评论,说:

<块引用>

(...) 它正在工作.但是 Microsoft Word 应用程序正在弹出每一个字.有没有办法在没有的情况下获得拼写建议让微软应用程序窗口弹出??

就我个人而言,我没有遇到过这种行为(无论是 GetSpellingSuggestions 还是 Application 实例上可用的 CheckSpelling 方法).

但是,如果您在 Document 实例上调用 CheckSpelling,它将如文档中所述,如果有一个或多个,则显示拼写对话框找到拼写错误的单词(假设您在构建 Word Application 实例期间,将 Visible 属性分配给 true - 否则,它将等待在后台输入导致应用程序冻结").

I am writing a spell check application in C# using word.dll (the Word Interop API).

I want to check which spellings are incorrect and accordingly get suggestions for the incorrect words.

I got a sample code from the net and I cannot understand the parameters for the following command:

Microsoft.Office.Interop.Word._Application.GetSpellingSuggestions
   (string, ref object, ref object, ref object, ref object, ref object, 
       ref object, ref object, ref object, ref object, ref object, ref object, 
       ref object, ref object)

I would just like to know what do all the ref objects imply? I want to know their meaning.

解决方案

I worked with this the other day and thought I would like to share my findings and add a bit to the already given answers.

You ask:

I want to check which spellings are incorrect and accordingly get suggestions for the incorrect words.

(...)

I would just like to know what do all the "ref object"s imply? I want to know their meaning.

The short answer to this is - look in the documentation.

To show you how the GetSpellingSuggestions method can be used in a more complete context, I have included a sample program in the following. Please note that you can change the desired proofing language using the language variable. The code goes as follows:

using System;
using Microsoft.Office.Interop.Word;

namespace WordStack
{
    public class Program
    {
        private static void Main()
        {
            // Create a new Word application instance (and keep it invisible)
            var wordApplication = new Application() { Visible = false };

            // A document must be loaded
            var myDocument = wordApplication.Documents.Open(@"C:...myDoc.docx");

            // Set the language
            var language = wordApplication.Languages[WdLanguageID.wdEnglishUS];

            // Set the filename of the custom dictionary
            // -- Based on:
            // http://support.microsoft.com/kb/292108
            // http://www.delphigroups.info/2/c2/261707.html
            const string custDict = "custom.dic";

            // Get the spelling suggestions
            var suggestions = wordApplication.GetSpellingSuggestions("overfloww", custDict, MainDictionary: language.Name);

            // Print each suggestion to the console
            foreach (SpellingSuggestion spellingSuggestion in suggestions)
                Console.WriteLine("Suggested replacement: {0}", spellingSuggestion.Name);

            Console.ReadLine();
            wordApplication.Quit();
        }
    }
}

... which give me the following three suggestions: overflow, overflows, and overflown.

The given sample has been implemented using .NET 4.5 and version 15 of the Word Interop API (Office 2013).

Please notice that the given sample also solves your comment to one of the already given answers, saying:

(...) It is working. But the Microsoft Word application is popping up for every word. Is there any way to get spelling suggestion without letting the Microsoft application window pop up??

Personally, I have not experienced that behavior (neither with the GetSpellingSuggestions nor the CheckSpelling methods available on the Application instance).

However, if you invoke CheckSpelling on a Document instance, it will, as covered in the documentation, display the Spelling dialog if one or more misspelled words are found (given that you, during construction of the Word Application instance, assigned the Visible property to true - otherwise, it will await input in the background causing the application to "freeze").

这篇关于使用 Word Interop 在 C# 中进行拼写检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆