Pages

Monday, 25 February 2013

Spell Check in C#


//add reference
Microsoft.Office.Interop.Word.dll

usingSystem;
using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Word;

namespace GrammerMistakes

{

public class WordProofing

{

private Application _wordApp;

private readonly Document _wordDoc;

private static object _oEndOfDoc = "\\endofdoc";

private string str;

public WordProofing()

{

_wordApp =
new Application { Visible = false };

_wordDoc = _wordApp.Documents.Add();

}

public void Close()

{

_wordDoc.Close(
WdSaveOptions.wdDoNotSaveChanges);

_wordApp.Quit();

}

public string Proof(string proofText)

{

Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range;

wRng.Text = proofText;

ProofreadingErrors spellingErros = wRng.SpellingErrors;

foreach (Range spellingError in spellingErros)

{

SpellingSuggestions spellingSuggestions =

_wordApp.GetSpellingSuggestions(spellingError.Text, IgnoreUppercase: true);

spellingError.Text = "";

foreach (SpellingSuggestion spellingSuggestion in spellingSuggestions)

{

if (proofText == spellingSuggestion.Name)

return "";

spellingError.Text = spellingSuggestion.Name;

str = spellingSuggestion.Name;

break;

}

}

//str = wRng.Text;

wRng.Text = "";

return str;

}

}

}