Click here to Skip to main content
12,401,628 members (57,866 online)
Click here to Skip to main content
Add your own
alternative version

Stats

186.4K views
7.4K downloads
74 bookmarked
Posted

Using Word's spellchecker in C#

, 17 Jul 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on using Word's spellchecker from a textbox in C#

Introduction

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

Background

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) 
    { 
        app.Visible=false; 
        // 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 "; 
        else 
            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; 
    } 
    else 
        lLbl.Text = "Textbox is empty"; 
    object saveChanges = false; 
    object originalFormat = Missing.Value; 
    object routeDocument = Missing.Value; 
    app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); 
}

License

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionBring Spell Check POP-UP to the front. Pin
DerrellGore1-Jun-15 10:09
memberDerrellGore1-Jun-15 10:09 
QuestionSpellChecker goes to the back Pin
DerrellGore29-May-15 4:22
memberDerrellGore29-May-15 4:22 
GeneralThanks, this was plug and play... Pin
John Ritchie16-Sep-14 11:01
memberJohn Ritchie16-Sep-14 11: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 3:43
professionalbgsjust24-Aug-13 3:43 
QuestionIs it possible to have multiple dictionarys ? Pin
bgsjust24-Aug-13 0:45
professionalbgsjust24-Aug-13 0:45 
QuestionError in Source code Pin
hardiktrivedihart2-Aug-13 6:22
memberhardiktrivedihart2-Aug-13 6:22 
GeneralMy vote of 4 Pin
Amsath ali26-Apr-11 19:08
memberAmsath ali26-Apr-11 19:08 
Generalis there a updated version of libraries Pin
mahesh152925-May-10 3:22
membermahesh152925-May-10 3:22 
QuestionAdd/Remove entries from default dictionary? Pin
dadada71118-May-10 23:23
memberdadada71118-May-10 23:23 
GeneralWord cannot save this file because it is already open elsewhere Pin
Member 361523317-Aug-09 2:12
memberMember 361523317-Aug-09 2:12 
GeneralUse the Open Office Spell Checker from .NET and get no licence problems Pin
Thomas Maierhofer21-Jul-09 1:24
memberThomas Maierhofer21-Jul-09 1:24 
GeneralRe: Use the Open Office Spell Checker from .NET and get no licence problems Pin
RealSkydiver13-Nov-09 0:43
memberRealSkydiver13-Nov-09 0:43 
GeneralRe: Use the Open Office Spell Checker from .NET and get no licence problems Pin
Thomas Maierhofer14-Nov-09 2:50
memberThomas Maierhofer14-Nov-09 2:50 
Generalcount is missleading on the number of errors actually corrected Pin
wleon14-Nov-08 6:13
memberwleon14-Nov-08 6:13 
QuestionHang? Pin
kikytan20-Apr-08 21:07
memberkikytan20-Apr-08 21:07 
AnswerRe: Hang? Pin
mnevis2-Jul-08 6:29
membermnevis2-Jul-08 6:29 
GeneralRe: Hang? Pin
davidc@innosphere.ca6-Aug-08 4:40
memberdavidc@innosphere.ca6-Aug-08 4:40 
AnswerRe: Hang? Pin
felchaal28-Aug-08 8:44
memberfelchaal28-Aug-08 8:44 
GeneralRe: Hang? Pin
pronning30024-Aug-10 0:41
memberpronning30024-Aug-10 0:41 
Generalspell-checking service Pin
hnipak19-Oct-06 3:56
memberhnipak19-Oct-06 3:56 
GeneralMore than just the count Pin
routsoja5-Jun-06 7:32
memberroutsoja5-Jun-06 7:32 
Generalspell check for website without it's dialog Pin
hala_ref3at3-May-06 11:18
memberhala_ref3at3-May-06 11:18 
Questionhow to have rtf style in richtextbox? Pin
hduani7-Nov-05 2:02
memberhduani7-Nov-05 2:02 
GeneralThe screen splash after spellcheck completed Pin
quangvinh19-Oct-05 1:02
memberquangvinh19-Oct-05 1:02 
GeneralRe: The screen splash after spellcheck completed Pin
gugaime15-Aug-06 19:20
membergugaime15-Aug-06 19:20 
GeneralRe: The screen splash after spellcheck completed Pin
shahbaz rehman15-Apr-08 3:01
membershahbaz rehman15-Apr-08 3:01 
Generalspell check for website Pin
sb785-Jul-05 11:40
membersb785-Jul-05 11:40 
GeneralRe: spell check for website Pin
nyland6-Jul-05 6:47
membernyland6-Jul-05 6:47 
GeneralRe: spell check for website Pin
sb786-Jul-05 10:18
membersb786-Jul-05 10:18 
GeneralRe: spell check for website Pin
anit10-Jul-05 13:08
memberanit10-Jul-05 13:08 
GeneralCreating a Custom Spell-Checker with Word 2003 Pin
Wagner DosAnjos23-Mar-05 9:47
memberWagner DosAnjos23-Mar-05 9:47 
GeneralEULA Pin
Kant18-Jul-03 8:17
memberKant18-Jul-03 8:17 
GeneralRe: EULA Pin
reinux11-Nov-04 11:33
memberreinux11-Nov-04 11:33 
GeneralRe: EULA Pin
CSpotkill13-Nov-04 21:36
memberCSpotkill13-Nov-04 21:36 
GeneralRe: EULA Pin
James S. Taylor5-Jun-07 12:44
memberJames S. Taylor5-Jun-07 12:44 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 18 Jul 2003
Article Copyright 2003 by nyland
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid