Wednesday 12 August 2015

Spell Checker in C#

Hi, 

In this tutorial, we will learn how to add spell check functionality in C# using VSTO (Visual Studio Tool for Office). For that you have to install Microsoft Word on your computer.

You have to add reference for the following namespace in the code.

using System.Reflection;
using Microsoft.Office.Interop.Word;

The working code snippet is as follow. It takes string to test spell check in it.
string s1 = textBox1.Text;

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word._Document doc = app.Documents.Add();

doc.Words.First.InsertBefore(s1);

Microsoft.Office.Interop.Word.ProofreadingErrors errors = doc.SpellingErrors;

int errorCount = errors.Count;

doc.CheckSpelling(Missing.Value, true, false);

app.Quit(false);

textBox3.Text = errorCount.ToString();
Demo application is shown with wrong text.
Application with wrong text
Windows showing wrong word as red highlighted text.
Word plugin checking word spells
Total error count is being showed at the end.
Application showing total number of errors

The source code of this application can be obtained through this link.

No comments:

Post a Comment