Spell Checking in C# Using Word Interop

后端 未结 3 1365
灰色年华
灰色年华 2020-12-16 07:09

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 s

相关标签:
3条回答
  • 2020-12-16 07:23
        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
    
    )
    
    0 讨论(0)
  • 2020-12-16 07:35

    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.

    You ask:

    I want to check which spellings are incorrect and accordingly get suggestions for the incorrect words.

    (...)

    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.

    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.

    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:

    (...) 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??

    Personally, I have not experienced that behavior (neither with the GetSpellingSuggestions nor the CheckSpelling methods available on the Application instance).

    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").

    0 讨论(0)
  • 2020-12-16 07:42

    Update: So it seems you need to get the first spelling suggestion from word. I checked this article and I deduce you would need to do something like this:

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

    So from the msdn docs:

    Syntax 1

    expression.GetSpellingSuggestions(CustomDictionary, IgnoreUppercase, 
        MainDictionary, SuggestionMode, CustomDictionary2  CustomDictionary10)
    

    Result: Returns a SpellingSuggestions collection that represents the words suggested as spelling replacements for the first word in the specified range.

    Syntax 2

    expression.GetSpellingSuggestions(Word, CustomDictionary, IgnoreUppercase,
     MainDictionary, SuggestionMode, CustomDictionary2  CustomDictionary10)
    

    Result:Returns a SpellingSuggestions collection that represents the words suggested as spelling replacements for a given word.

    Note: If you use anything earlier than .NET4 then you'll have to use Missing.Value for the parameters you want empty/null. As of .NET4 we've got optional parameters and when you add a reference to the Office Library, the interop wrapper will have overloads based on the optional parameters.

    0 讨论(0)
提交回复
热议问题