使用Word Interop在C#中进行拼写检查 [英] Spell Checking in C# Using Word Interop
问题描述
我正在使用word.dll
(Word Interop API)在C#中编写拼写检查应用程序.
I am writing a spell check application in C# using word.dll
(the Word Interop API).
我想检查哪些拼写不正确,并因此获得有关不正确单词的建议.
I want to check which spellings are incorrect and accordingly get suggestions for the incorrect words.
我从网上得到了一个示例代码,但无法理解以下命令的参数:
I got a sample code from the net and I cannot understand the parameters for the following command:
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
意味着什么?我想知道他们的意思.
I would just like to know what do all the ref object
s imply? I want to know their meaning.
推荐答案
前几天,我与之一起工作,并认为我想分享自己的发现,并为已经给出的答案添加一些内容.
I worked with this the other day and thought I would like to share my findings and add a bit to the already given answers.
您问:
我想检查哪些拼写不正确并相应得到 有关不正确单词的建议.
I want to check which spellings are incorrect and accordingly get suggestions for the incorrect words.
(...)
我只想知道所有"ref对象"的含义是什么?我想 知道它们的含义.
I would just like to know what do all the "ref object"s imply? I want to know their meaning.
The short answer to this is - look in the documentation.
为了向您展示如何在更完整的上下文中使用GetSpellingSuggestions
方法,下面提供了一个示例程序.请注意,您可以使用language
变量来更改所需的校对语言.代码如下:
To show you how the GetSpellingSuggestions
method can be used in a more complete context, I have included a sample program in the following. Please note that you can change the desired proofing language using the language
variable. The code goes as follows:
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();
}
}
}
...这给了我以下三个建议:溢出,溢出和溢出.
... which give me the following three suggestions: overflow, overflows, and overflown.
给定的示例已使用.NET 4.5和Word Interop API的版本15(Office 2013)实现.
The given sample has been implemented using .NET 4.5 and version 15 of the Word Interop API (Office 2013).
请注意,给出的样本还会解决您对已经给出的答案之一的评论,说:
Please notice that the given sample also solves your comment to one of the already given answers, saying:
(...)正在工作.但是Microsoft Word应用程序正在弹出 每一个字.有没有办法得到拼写建议而无需 让Microsoft应用程序窗口弹出??
(...) It is working. But the Microsoft Word application is popping up for every word. Is there any way to get spelling suggestion without letting the Microsoft application window pop up??
我个人还没有遇到这种行为(Application
实例上的GetSpellingSuggestions
或CheckSpelling
方法都没有).
Personally, I have not experienced that behavior (neither with the GetSpellingSuggestions
nor the CheckSpelling
methods available on the Application
instance).
但是,如果在Document
实例上调用CheckSpelling
,它将Application实例的构造,将Visible
属性分配给true
-否则,它将在后台等待输入,导致应用程序冻结".)
However, if you invoke CheckSpelling
on a Document
instance, it will, as covered in the documentation, display the Spelling dialog if one or more misspelled words are found (given that you, during construction of the Word Application
instance, assigned the Visible
property to true
- otherwise, it will await input in the background causing the application to "freeze").
这篇关于使用Word Interop在C#中进行拼写检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!