使用Word Interop在C#中进行拼写检查

4

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

我想检查哪些拼写不正确,并相应地获得有关错误单词的建议。

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

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)

我想知道所有ref object的含义是什么?我想了解它们的意思。

3个回答

4
我最近在处理这个问题,并想分享一下我的发现并补充一些已有答案的内容。
您的问题是:

我想检查哪些拼写错误并相应地获得这些错误单词的建议。

(...)

我只想知道所有“ref object”都意味着什么? 我要知道他们的含义。

简短的回答是 - 请参阅文档
为了向您展示GetSpellingSuggestions方法如何在更完整的上下文中使用,我提供了以下示例程序。 请注意,您可以使用language变量更改所需的校对语言。 代码如下:
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();
        }
    }
}

...这让我得出以下三个建议:overflowoverflowsoverflown

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

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

(...)它正在工作。但是Microsoft Word应用程序会弹出每个单词。有没有办法获得拼写建议而不让Microsoft应用程序窗口弹出??

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

然而,如果在Document实例上调用CheckSpelling,它将会在发现一个或多个拼写错误时显示拼写对话框(如文档中所述)(前提是在构建Word Application实例时为Visible属性分配了true——否则,它将在后台等待输入,导致应用程序“冻结”)。

3

更新: 看起来您需要从单词中获取第一个拼写建议。我查看了这篇文章,推断您需要执行以下操作:

Word.SpellingSuggestions listOfSuggestions = 
                                  app.GetSpellingSuggestions(searchStr);
listOfSuggestions.Items[0].Name;//should contain the first suggestion

msdn文档中可以得知:
语法1
expression.GetSpellingSuggestions(CustomDictionary, IgnoreUppercase, 
    MainDictionary, SuggestionMode, CustomDictionary2  CustomDictionary10)
结果:返回一个SpellingSuggestions集合,该集合代表建议作为指定范围内第一个单词的拼写替换的单词。

语法2

expression.GetSpellingSuggestions(Word, CustomDictionary, IgnoreUppercase,
 MainDictionary, SuggestionMode, CustomDictionary2  CustomDictionary10)
结果:返回一个SpellingSuggestions集合,该集合代表给定单词的拼写替换建议。 注意:如果您使用的是早于.NET4的版本,则必须对您想要的空/空值参数使用Missing.Value。从.NET4开始,我们有了可选参数,当您添加对Office库的引用时,互操作包装器将基于可选参数提供重载。

非常感谢。实际上,我想通过代码获取第一个拼写建议,而不是通过 Microsoft Word 打开建议窗口。我该如何做到这一点? - Prachi Patil
非常感谢,它正在工作。但是每个单词都会弹出Microsoft Word应用程序。有没有办法在不让Microsoft应用程序窗口弹出的情况下获得拼写建议? - Prachi Patil

0
    SpellingSuggestions GetSpellingSuggestions(
        string Word,
        ref Object CustomDictionary,
        ref Object IgnoreUppercase,
        ref Object MainDictionary,
        ref Object SuggestionMode,
        ref Object CustomDictionary2,
        ref Object CustomDictionary3,
        ref Object CustomDictionary4,
        ref Object CustomDictionary5,
        ref Object CustomDictionary6,
        ref Object CustomDictionary7,
        ref Object CustomDictionary8,
        ref Object CustomDictionary9,
        ref Object CustomDictionary10

)

1
为什么不参考文档呢? - Lasse Christiansen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接