Click here to Skip to main content
Licence CPOL
First Posted 17 Jul 2003
Views 139,699
Bookmarked 67 times

Using Word's spellchecker in C#

By | 17 Jul 2003 | Article
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)

About the Author

nyland

Web Developer

Canada Canada

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberAmsath ali19:08 26 Apr '11  
Generalis there a updated version of libraries Pinmembermahesh15293:22 25 May '10  
QuestionAdd/Remove entries from default dictionary? Pinmemberdadada71123:23 18 May '10  
GeneralWord cannot save this file because it is already open elsewhere PinmemberMember 36152332:12 17 Aug '09  
GeneralUse the Open Office Spell Checker from .NET and get no licence problems PinmemberThomas Maierhofer1:24 21 Jul '09  
GeneralRe: Use the Open Office Spell Checker from .NET and get no licence problems PinmemberRealSkydiver0:43 13 Nov '09  
GeneralRe: Use the Open Office Spell Checker from .NET and get no licence problems PinmemberThomas Maierhofer2:50 14 Nov '09  
Generalcount is missleading on the number of errors actually corrected Pinmemberwleon6:13 14 Nov '08  
Generalcheckspelling dialog not appearing PinmemberMember 175104013:01 14 Aug '08  
QuestionHang? Pinmemberkikytan21:07 20 Apr '08  
AnswerRe: Hang? Pinmembermnevis6:29 2 Jul '08  
GeneralRe: Hang? Pinmemberdavidc@innosphere.ca4:40 6 Aug '08  
AnswerRe: Hang? Pinmemberfelchaal8:44 28 Aug '08  
GeneralRe: Hang? Pinmemberpronning3000:41 24 Aug '10  
Generalspell-checking service Pinmemberhnipak3:56 19 Oct '06  
GeneralMore than just the count Pinmemberroutsoja7:32 5 Jun '06  
Generalspell check for website without it's dialog Pinmemberhala_ref3at11:18 3 May '06  
Questionhow to have rtf style in richtextbox? Pinmemberhduani2:02 7 Nov '05  
GeneralThe screen splash after spellcheck completed Pinmemberquangvinh1:02 19 Oct '05  
GeneralRe: The screen splash after spellcheck completed Pinmembergugaime19:20 15 Aug '06  
GeneralRe: The screen splash after spellcheck completed Pinmembershahbaz rehman3:01 15 Apr '08  
Generalspell check for website Pinmembersb7811:40 5 Jul '05  
GeneralRe: spell check for website Pinmembernyland6:47 6 Jul '05  
GeneralRe: spell check for website Pinmembersb7810:18 6 Jul '05  
Hi Nyland,
Thanks for the codes. I tried this in C#. I have a reference set to interop.word.dll and at the point where I create a word application as
 
Word.Application app = new Word.Application();
 
it throws an authentication error:
Access is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.UnauthorizedAccessException: Access is denied.
 
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
 
To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
 
Could you please give any suggestions? WOuld it be possible to use Microsoft word.dll for spell check in a web application wihtout creating a security hole?
 
Thanks again,
Saritha

GeneralRe: spell check for website Pinmemberanit13:08 10 Jul '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 18 Jul 2003
Article Copyright 2003 by nyland
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid