5,448,416 members and growing! (20,548 online)
Email Password   helpLost your password?
Languages » C# » COM Interop     Intermediate License: The Code Project Open License (CPOL)

Using Word's spellchecker in C#

By nyland

An article on using Word's spellchecker from a textbox in C#
C#, Windows, .NET 1.0, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 17 Jul 2003
Updated: 17 Jul 2003
Views: 73,480
Bookmarked: 39 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 4.97 Rating: 4.23 out of 5
0 votes, 0.0%
1
1 vote, 6.7%
2
1 vote, 6.7%
3
4 votes, 26.7%
4
9 votes, 60.0%
5

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)

About the Author

nyland


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

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Subject  Author Date 
Generalcheckspelling dialog not appearingmemberMember 175104014:01 14 Aug '08  
GeneralHang?memberkikytan22:07 20 Apr '08  
GeneralRe: Hang?membermnevis7:29 2 Jul '08  
GeneralRe: Hang?memberdavidc@innosphere.ca5:40 6 Aug '08  
GeneralRe: Hang?memberfelchaal9:44 28 Aug '08  
Generalspell-checking servicememberhnipak4:56 19 Oct '06  
GeneralMore than just the countmemberroutsoja8:32 5 Jun '06  
Generalspell check for website without it's dialogmemberhala_ref3at12:18 3 May '06  
Generalhow to have rtf style in richtextbox?memberhduani3:02 7 Nov '05  
GeneralThe screen splash after spellcheck completedmemberquangvinh2:02 19 Oct '05  
GeneralRe: The screen splash after spellcheck completedmembergugaime20:20 15 Aug '06  
GeneralRe: The screen splash after spellcheck completedmembershahbaz rehman4:01 15 Apr '08  
Generalspell check for websitemembersb7812:40 5 Jul '05  
GeneralRe: spell check for websitemembernyland7:47 6 Jul '05  
GeneralRe: spell check for websitemembersb7811:18 6 Jul '05  
GeneralRe: spell check for websitememberanit14:08 10 Jul '05  
GeneralCreating a Custom Spell-Checker with Word 2003memberWagner DosAnjos10:47 23 Mar '05  
GeneralEULAmemberKant9:17 18 Jul '03  
GeneralRe: EULAmemberreinux12:33 11 Nov '04  
GeneralRe: EULAmemberCSpotkill22:36 13 Nov '04  
GeneralRe: EULAmemberJames S. Taylor13:44 5 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jul 2003
Editor: Smitha Vijayan
Copyright 2003 by nyland
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project