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

我正在使用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得到第一个拼写建议。 我检查了这篇文章,我推断你需要做这样的事情:

 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包装器将根据可选参数进行重载。

前几天我和这个人一起工作,并且认为我想分享我的发现并为已经给出的答案添加一些内容。

你问:

我想检查哪些拼写不正确,并据此获得错误单词的建议。

(……)

我想知道所有“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(); } } } 

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

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

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

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

就个人而言,我没有经历过这种行为(既没有GetSpellingSuggestions也没有在Application实例上提供的CheckSpelling方法)。

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

  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 )