Spell Checking in C# Using Word Interop

后端 未结 3 1479
灰色年华
灰色年华 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: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").

提交回复
热议问题