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

本文关键字:检查 Word Interop 使用 | 更新日期: 2023-09-27 18:27:57

我正在使用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都意味着什么?我想知道它们的含义。

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

前几天我处理过这个问题,我想分享我的发现,并在已经给出的答案上添加一些内容。

你问:

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

(…)

我只是想知道所有"ref对象"的含义是什么?我想要了解它们的含义。

对此的简短回答是——查看文档。

为了向您展示如何在更完整的上下文中使用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();
        }
    }
}

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

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

请注意,给定的样本也解决了您对其中一个已经给出的答案的评论,即:

(…)它正在工作。但微软Word应用程序正在为每一个字。有什么方法可以在没有拼写建议的情况下得到拼写建议吗是否允许弹出Microsoft应用程序窗口??

就我个人而言,我从未经历过这种行为(GetSpellingSuggestionsCheckSpelling方法在Application实例上都不可用)。

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

更新:所以你们似乎需要从单词中得到第一个拼写建议。我检查了这篇文章,我推断你需要做这样的事情:

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作为所需的empty/null参数。从.NET4开始,我们有可选参数,当您添加对Office库的引用时,互操作包装器将基于可选参数重载。

    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
)