Click here to Skip to main content
6,594,432 members and growing! (14,998 online)
Email Password   helpLost your password?
Languages » VB.NET » HowTo     Intermediate License: The Code Project Open License (CPOL)

Spell Check in .NET without adding a reference ahead of time - version agnostic!

By puschusb

Run Spell Check in your .NET apps through any version of Word, without having to make a reference at design time.
VB, .NET (.NET 2.0, .NET 3.0, .NET 3.5), Visual Studio (VS2005, VS2008), COM, COM+, Dev, Design
Posted:12 Sep 2008
Views:5,421
Bookmarked:8 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 0.95 Rating: 1.58 out of 5
2 votes, 50.0%
1

2

3
1 vote, 25.0%
4
1 vote, 25.0%
5

Introduction

Very often, when developing software, we have to include a spelling and grammar check function, and don't want to shell out the cash for a proprietary solution. We search the web and find ways to incorporate the MS Word interop capabilities - it always starts with, create a solution, add a reference.

Then, we build it and run it, and it works great. Then, we run it on someone else's computer who either doesn't have MS Word or has a different version, and we have all kinds of problems - assemblies not found in the GAC, etc. Why can't we add the reference at runtime so we can use any version of Word?

Using the System.Reflection namespace, we can!

Background

You should be familiar with the MS Office interop services, see a few other spell check examples if you are not. Bits and pieces of this were inspired by other CodeProject articles, and it is covered much more in depth in them. This article will focus on doing the same thing, but with System.Reflection rather than adding a reference ahead of time.

Using the code

To use the code, just call the Sub and pass it a TextBox.

There are two Imports to be used...

Imports System.Runtime.InteropServices 
Imports System.Reflection

and now the magic:

Private Sub SpellAndGrammarCheck(ByVal YourTextbox As TextBox)

    Try
        'Rather than giving it a type right now, we just give 
        'it generic object status - same below 
        Dim objWord As Object = Nothing
        Dim objDoc As Object = Nothing
        Dim objData As IDataObject = Nothing

        If YourTextbox.Text = "" Then
        ' if there's nothing in your textbox, there's nothign to do
            Exit Sub
        End If

        objWord = System.Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"))

        Dim objDocuments As Object

        objDocuments = objWord.[GetType]().InvokeMember("Documents", _
                       BindingFlags.[Default] Or BindingFlags.GetProperty, _
                       Nothing, objWord, Nothing)


        objDoc = objDocuments.[GetType]().InvokeMember("Add", BindingFlags.[Default] _
                 Or BindingFlags.InvokeMethod, Nothing, objDocuments, Nothing)
        objWord.Visible = False

        objWord.WindowState = 0
        objWord.Top = -3000
        Clipboard.SetDataObject(YourTextbox.Text)

            With objDoc
                .Content.Paste()
                .Activate()
                .CheckGrammar()
                .Content.Copy()
                objData = Clipboard.GetDataObject
                If objData.GetDataPresent(DataFormats.Text) Then
                    YourTextbox.Text = CType(objData.GetData(DataFormats.Text), String)
                End If
                .Saved = True
                .Close()
            End With
            objWord.Quit()
            ' MsgBox("Spelling check complete!")
            SPELLWORKING = True
        Catch COMEcep As COMException
            MsgBox("MS Word must be installed to perform spelling checks", , _
                   "Spell check is not available")
            SPELLWORKING = False

        Catch ex As Exception
            SPELLWORKING = False

            MsgBox("Error, Make sure you have MS word installed.", , ex.Message)

        End Try

Points of interest

I've had one instance of this causing a 'hang', but when the program restarted, it was fine... I've tested it with Word XP, 2003, and 2007. Hope it helps you!

License

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

About the Author

puschusb


Member

Location: United States United States

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

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

PermaLink | Privacy | Terms of Use
Last Updated: 12 Sep 2008
Editor: Smitha Vijayan
Copyright 2008 by puschusb
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project