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

时间:2012-03-15 11:23:27

标签: c# ms-word office-interop spell-checking

我正在使用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 个答案:

答案 0 :(得分:4)

前几天我与之合作,并认为我想分享我的发现并为已经给出的答案添加一些内容。

你问:

  

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

     

(...)

     

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

对此的简短回答是 - 查看the documentation

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

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

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

请注意,给定的样本也会解决您对已经给出的答案之一的评论,并说:

  

(...)它正在发挥作用。但是Microsoft Word应用程序正在涌现   每一个字。没有任何方法可以获得拼写建议   让Microsoft应用程序窗口弹出?

就个人而言,我没有遇到过这种行为(GetSpellingSuggestions实例上的CheckSpellingApplication方法都没有。

但是,如果您在CheckSpelling实例上调用Document,如果找到一个或多个拼写错误的单词,它将as covered in the documentation显示拼写对话框(假设您在施工期间)单词Application实例,将Visible属性分配给true - 否则,它将在后台等待输入,导致应用程序“冻结”。

答案 1 :(得分:3)

更新: 因此,您似乎需要从word获得第一个拼写建议。我检查了这篇文章,我推断你需要做这样的事情:

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

所以来自msdn docs

语法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库的引用时,interop包装器将根据可选参数进行重载。

答案 2 :(得分: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

)
相关问题