【问题标题】:Spell Checking in C# Using Word Interop在 C# 中使用 Word 互操作进行拼写检查
【发布时间】:2012-03-31 21:35:12
【问题描述】:

我正在使用word.dll(Word 互操作 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 objects 的含义是什么?我想知道它们的含义。

【问题讨论】:

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


    【解决方案1】:

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

    你问:

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

    (...)

    我只想知道所有“参考对象”的含义是什么?我想要 了解它们的含义。

    对此的简短回答是 - 查看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();
            }
        }
    }
    

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

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

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

    (...) 它正在工作。但是 Microsoft Word 应用程序正在弹出 每一个字。有没有什么方法可以得到拼写建议 让微软应用程序窗口弹出??

    就我个人而言,我没有经历过这种行为(GetSpellingSuggestionsApplication 实例上可用的 CheckSpelling 方法都没有)。

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

    【讨论】:

      【解决方案2】:

      更新: 因此,您似乎需要从 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 库的引用时,互操作包装器将根据可选参数进行重载。

      【讨论】:

      • 非常感谢。实际上,我想通过代码获得第一个拼写建议,而不是通过 microsoft word 打开建议窗口。我怎么能做到这一点??
      • 非常感谢......它正在工作。但是每个单词都会弹出 Microsoft Word 应用程序。有什么方法可以在不弹出微软应用程序窗口的情况下获得拼写建议??
      【解决方案3】:
          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
      
      )
      

      【讨论】:

      • 为什么不参考文档呢?
      猜你喜欢
      • 1970-01-01
      • 2012-08-20
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多