C# Word Interop - Spell Checking in a Certain Lang

2019-05-05 17:20发布

问题:

For a customer of mine I need to force the spell checking in a certain language.

I have explored the MSDN documentation and found that when calling the CheckSpelling() method in the active document, it will invoke the spelling check. This method has parameters for custom dictionaries.

My problem is that I can't find anything about those dictionaries or how to use them.

Also there is still the possibility that there is of course another way to do this.

Can anybody boost me in the right direction?

回答1:

Found my solution:

foreach (Range range in activeDocument.Words)
{
    range.LanguageID = WdLanguageID.wdFrenchLuxembourg;
}

Edit after comment

Since my activedocument is in a variable I seem to lose the static Range property. I found a work arround by doing the following. (lan is my variable where i keep my WdLanguageId)

object start = activeDocument.Content.Start;
object end = activeDocument.Content.End;

activeDocument.Range(ref start, ref end).LanguageID = lan;

thanks @Adrianno for all the help!



回答2:

The Spell Checker uses the language of the text to select rules and dictionaries (look here to check how it works).

You have to set the text language to what you need and then SC will use that language. Follow this link for more details:

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.language.aspx



回答3:

I have been working with this lately and thought I would add a bit to the already given answers.

To get a list of spelling errors in the document for a certain language, doing the following would get you going:

// Set the proofing language
myDocument.Content.LanguageID = WdLanguageID.wdDanish;

// Get the spelling errors (returns a ProofreadingErrors collection)
var errors = myDocument.SpellingErrors;

// There is no "ProofreadingError" object -> errors are accessed as Ranges
foreach (Range proofreadingError in errors)
    Console.WriteLine(proofreadingError.Text);

As pointed out by Adriano, the key is to specify the language of the document content at first, and then you can access the spelling errors for the given language. I have tested this (Word Interop API version 15, Office 2013), and it works.

If you want to get suggestions for each of the misspelled words as well, I suggest you take a look at my previous answer to that issue: https://stackoverflow.com/a/14202099/700926

In that answer I provide sample code as well as links to relevant documentation for how that is done. In particular, the sample covers how to carry out spell checking of a given word in a certain language (of your choice) using Word Interop. The sample also covers how to access the suggestions returned by Word.

Finally, I have a couple of notes:

  • In contrast to the current accepted answer (your own) - this approach is much faster since it do not have to iterate through each word. I have been working with Word Interop for reports (100+ pages) and trust me, you don't want to sit and wait for that iteration to finish.
  • Information regarding the SpellingErrors property can be found here.
  • Information regarding the non-existence of a ProofreadingError object can be found here.


回答4:

Never user foreach statements when accessing Office object. Most of the Office objects are COM object, and using foreach leads to memory leaks.

The following is a piece of working code

    Microsoft.Office.Interop.Word.ProofreadingErrors errorCollection = null;

    try
    {
        errorCollection = Globals.ThisAddIn.Application.ActiveDocument.SpellingErrors;

     // Indexes start at 1 in Office objects
        for (int i = 1; i <= errorCollection .Count; i++)
        {
            int start =  errorCollection[i].Start;
            int end = errorCollection[i].End;

        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);

    }
    finally
    {
        // Release the COM objects here 
        // as finally shall be always called

        if (errorCollection != null)
        {
            Marshal.ReleaseComObject(errorCollection);
            errorCollection = null;
        }

    }