Spell check by changing Directory language [word 2007 component]

This code gives you the functionality to check the spelling mistakes for given string.

Also you can set the language of the directory as you wish.

Code is tested using word 2007 component only. May not work lower versions.



//Declare the following variables as a class level varialbles

private static Object m_emptyItem = System.Reflection.Missing.Value;

private static Object m_oNull = null;

private static Object m_oTrue = true;

private static Object m_oFalse = false;

private static Object m_oAlwaysSuggest = true;

private static Object m_oIgnoreUpperCase = false;


//Pass the string to this method which will find the spelling mistakes in string
//Also you can set the dictionary language.
public static string CheckSpelling(string text)
{

// declare local variables to track error count and information
string correctedText = text;

Microsoft.Office.Interop.Word.Application wordApp = null;
_Document wordDoc = null;

try
{
// check for zero length content in text area
if (text.Length > 0)
{
// create an instance of a word application
wordApp = new Microsoft.Office.Interop.Word.Application();

// hide the MS Word document during the spellcheck
wordApp.Visible = false;

// create an instance of a word document
wordDoc = wordApp.Documents.Add(ref m_emptyItem, ref m_emptyItem, ref m_emptyItem, ref m_oFalse);

//To set the directory language
wordDoc.Content.LanguageID = WdLanguageID.wdEnglishUK;

// load the content written into the word doc
wordDoc.Words.First.InsertBefore(text);

// collect errors form new temporary document set to contain the content of this control
Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = wordDoc.SpellingErrors;

// execute spell check; assumes no custom dictionaries
wordDoc.CheckSpelling(ref m_oNull, ref m_oIgnoreUpperCase, ref m_oAlwaysSuggest, ref m_oNull, ref m_oNull, ref m_oNull, ref m_oNull, ref m_oNull, ref m_oNull, ref m_oNull, ref m_oNull, ref m_oNull);

// return corrected text to control's text area

Object first = 0;
Object last = wordDoc.Characters.Count - 1;

correctedText = wordDoc.Range(ref first, ref last).Text;
}
return correctedText;
}
finally
{
if (wordDoc != null)
wordDoc.Close(ref m_oFalse, ref m_emptyItem, ref m_emptyItem);
if (wordApp != null)
wordApp.Quit(ref m_oFalse, ref m_emptyItem, ref m_emptyItem);
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: