2012-03-15 77 views
3

我正在使用word.dll(Word Interop API)在C#中編寫拼寫檢查應用程序。在C#中使用Word進行拼寫檢查Interop

我想檢查哪些拼寫不正確,並據此獲取不正確詞彙的建議。

我從網上的示例代碼,我無法理解以下命令參數:

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小號意味着什麼呢?我想知道他們的意思。

回答

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 

) 
+1

爲什麼不參考文檔呢? – 2013-01-07 21:31:11

3

更新: 因此,看來你需要從單詞的第一個拼寫建議。我查了一下這篇文章,我推斷你需要做這樣的事情:

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包裝器將根據可選參數進行重載。

+0

非常感謝。其實我想通過代碼獲得第一個拼寫建議,而不是通過微軟的Word打開建議窗口。我將如何能夠做到這一點? – 2012-03-15 11:46:08

+0

@PrachiPatil我更新了我的答案。 – gideon 2012-03-15 13:45:58

+0

非常感謝你....它正在工作。但微軟的Word應用程序正在爲每一個字出現。有沒有辦法讓拼寫建議不讓微軟應用程序窗口彈出? – 2012-03-22 10:57:36

2

我在這一天工作過,並認爲我想分享我的發現,並添加一些已經給出的答案。

你問:

我要檢查它的拼寫不正確,因此得到不正確的話 建議。

(...)

我只是想知道什麼做所有的「參考對象」 S意味着什麼呢?我想要 知道他們的意思。

簡短的回答是 - 看看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(); 
     } 
    } 
} 

...它給我以下三點建議:溢出溢出飛越

給出的示例已經使用.NET 4.5和Word Interop API的版本15(Office 2013)實現。

請注意,在給定的樣本也解決您的評論對已經給出一個答案,他說:

(...)這是工作。但是微軟Word應用程序每個字都會彈出 。有沒有辦法讓拼寫建議沒有 讓微軟應用程序窗口彈出?

就個人而言,我沒有經歷過這種行爲(既不在Application實例可用GetSpellingSuggestions也不是CheckSpelling方法)。

但是,如果你在一個Document實例調用CheckSpelling,它將,as covered in the documentation,顯示拼寫對話框如果一個或多個拼錯的單詞被發現(因爲你,建築話語Application實例,在分配Visible屬性true - 否則,它將在後臺等待輸入,導致應用程序「凍結」)。