Click here to Skip to main content
15,169,285 members
Articles / Programming Languages / C#
Posted 17 Jul 2003


75 bookmarked

Using Word's spellchecker in C#

Rate me:
Please Sign up or sign in to vote.
4.44/5 (23 votes)
17 Jul 2003CPOL
An article on using Word's spellchecker from a textbox in C#

Image 1


This little function written in C# will invoke Microsoft Word's spellchecker and check the spelling of a given textbox.


I had written a program in C# in which a spellchecker could be useful. Since I had a hard time finding any information on the net or in the MSDN Library for .NET, on how to use Word's spellchecker from a C# application I was wondering if it was because it was a trivial affair to program, something only a few people would use or because it was actual difficult to program. I decided to dig deeper into it. A VBA script that I found on the net convinced me that it would be possible to find a solution. The following article in MSDN "AutoWord Sample: Demonstrates Automating Microsoft Word" pointed in the right direction and from there it was straight forward.

Using the code

Remember to reference the Microsoft Word xx Object Library. (Interop.Word.dll) There are so many properties and functions for the document object that if you want to use a similar logic for a RichTextBox it should be fairly simple.

using Word;
using System.Reflection;


private void button1_Click(object sender, System.EventArgs e) 
    fSpellCheck(textBox1 , label1 ); 
public void fSpellCheck(TextBox tBox, Label lLbl) 
    int iErrorCount = 0; 
    Word.Application app = new Word.Application(); 
    if (tBox.Text.Length > 0) 
        // Setting these variables is comparable
        // to passing null to the function. 
        // This is necessary because the C# null
        // cannot be passed by reference. 
        object template=Missing.Value; 
        object newTemplate=Missing.Value; 
        object documentType=Missing.Value; 
        object visible=true; 
        object optional = Missing.Value; 
        _Document doc = app.Documents.Add(ref template, 
           ref newTemplate, ref documentType, ref visible); 
        doc.Words.First.InsertBefore (tBox.Text ); 
        Word.ProofreadingErrors we = doc.SpellingErrors; 
        iErrorCount = we.Count; 
        doc.CheckSpelling( ref optional, ref optional, ref optional, 
            ref optional, ref optional, ref optional, ref optional, 
            ref optional, ref optional, ref optional, 
            ref optional, ref optional); 
        if (iErrorCount == 0) 
            lLbl.Text = "Spelling OK. No errors corrected "; 
        else if (iErrorCount == 1) 
            lLbl.Text = "Spelling OK. 1 error corrected "; 
            lLbl.Text = "Spelling OK. " + iErrorCount + 
                                    " errors corrected "; 
        object first=0; 
        object last=doc.Characters.Count -1; 
        tBox.Text = doc.Range(ref first, ref last).Text; 
        lLbl.Text = "Textbox is empty"; 
    object saveChanges = false; 
    object originalFormat = Missing.Value; 
    object routeDocument = Missing.Value; 
    app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Canada Canada
Working as a software developer - currently workflow management tools - and enjoying every minute of it.

Comments and Discussions

QuestionBring Spell Check POP-UP to the front. Pin
DerrellGore1-Jun-15 11:09
MemberDerrellGore1-Jun-15 11:09 
QuestionSpellChecker goes to the back Pin
DerrellGore29-May-15 5:22
MemberDerrellGore29-May-15 5:22 
GeneralThanks, this was plug and play... Pin
John Ritchie16-Sep-14 12:01
MemberJohn Ritchie16-Sep-14 12:01 
Questionhey this algorithm will make it work better because it checks for canceling and gives the number or errors corrected! ;) Pin
bgsjust24-Aug-13 4:43
professionalbgsjust24-Aug-13 4:43 
QuestionIs it possible to have multiple dictionarys ? Pin
bgsjust24-Aug-13 1:45
professionalbgsjust24-Aug-13 1:45 
QuestionError in Source code Pin
hardiktrivedihart2-Aug-13 7:22
Memberhardiktrivedihart2-Aug-13 7:22 
GeneralMy vote of 4 Pin
Amsath ali26-Apr-11 20:08
MemberAmsath ali26-Apr-11 20:08 
Generalis there a updated version of libraries Pin
mahesh152925-May-10 4:22
Membermahesh152925-May-10 4:22 
QuestionAdd/Remove entries from default dictionary? Pin
dadada71119-May-10 0:23
Memberdadada71119-May-10 0:23 
GeneralWord cannot save this file because it is already open elsewhere Pin
Sreejith Kulamgarath17-Aug-09 3:12
MemberSreejith Kulamgarath17-Aug-09 3:12 
GeneralUse the Open Office Spell Checker from .NET and get no licence problems Pin
Thomas Maierhofer (Tom)21-Jul-09 2:24
MemberThomas Maierhofer (Tom)21-Jul-09 2:24 
GeneralRe: Use the Open Office Spell Checker from .NET and get no licence problems Pin
RealSkydiver13-Nov-09 1:43
MemberRealSkydiver13-Nov-09 1:43 
GeneralRe: Use the Open Office Spell Checker from .NET and get no licence problems Pin
Thomas Maierhofer (Tom)14-Nov-09 3:50
MemberThomas Maierhofer (Tom)14-Nov-09 3:50 
Generalcount is missleading on the number of errors actually corrected Pin
wleon14-Nov-08 7:13
Memberwleon14-Nov-08 7:13 
Generalcheckspelling dialog not appearing Pin
Gijo_ Joseph14-Aug-08 14:01
MemberGijo_ Joseph14-Aug-08 14:01 
GeneralRe: checkspelling dialog not appearing Pin
i006-May-13 22:23
Memberi006-May-13 22:23 
QuestionHang? Pin
kikytan20-Apr-08 22:07
Memberkikytan20-Apr-08 22:07 
AnswerRe: Hang? Pin
mnevis2-Jul-08 7:29
Membermnevis2-Jul-08 7:29 
GeneralRe: Hang? Pin
davidc@innosphere.ca6-Aug-08 5:40
Memberdavidc@innosphere.ca6-Aug-08 5:40 
AnswerRe: Hang? Pin
felchaal28-Aug-08 9:44
Memberfelchaal28-Aug-08 9:44 
GeneralRe: Hang? Pin
pronning30024-Aug-10 1:41
Memberpronning30024-Aug-10 1:41 
Generalspell-checking service Pin
hnipak19-Oct-06 4:56
Memberhnipak19-Oct-06 4:56 
GeneralMore than just the count Pin
routsoja5-Jun-06 8:32
Memberroutsoja5-Jun-06 8:32 
Generalspell check for website without it's dialog Pin
hala_ref3at3-May-06 12:18
Memberhala_ref3at3-May-06 12:18 
Questionhow to have rtf style in richtextbox? Pin
hduani7-Nov-05 3:02
Memberhduani7-Nov-05 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.