Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCall Word to spell check from Visual Basic .NET
DescriptionThis example shows how to call Word to spell check from Visual Basic .NET.
KeywordsOffice, Word, spell, spellcheck, VB.NET
CategoriesOffice, VB.NET
 
This program requires a reference to the Word object model. Select Project/Add Reference. Click the COM tab and select "Microsoft Word 11.0 Object Library" (or whatever version you have on your system).

When you click the Check button, the program creates a Word.Application object and sets its Visible property to False. It uses that object's Documents.Add method to create a new Document object. It gets a Range object representing the Document's text and copies the text from TextBox1 into the Document.

Next the program activates the Document and calls its CheckSpelling method.

When CheckSpelling returns, the program copies the result back into TextBox1, removing any leading and trailing carriage returns and line feeds that Word added. Finally it closees the Document, telling it not to save the new text, and closes the Word server.

 
Private Sub btnCheck_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCheck.Click
    If TextBox1.Text.Length > 0 Then
        ' Make a Word server object.
        Dim word_server As New Word.Application

        ' Hide the server.
        word_server.Visible = False

        ' Make a Word Document.
        Dim doc As Word.Document = _
            word_server.Documents.Add()
        Dim rng As Word.Range

        ' Make a Range to represent the Document.
        rng = doc.Range()

        ' Copy the text into the Document.
        rng.Text = TextBox1.Text

        ' Activate the Document and call its CheckSpelling
        ' method.
        doc.Activate()
        doc.CheckSpelling()

        ' Copy the results back into the TextBox,
        ' trimming off trailing CR and LF characters.
        Dim chars() As Char = {CType(vbCr, Char), _
            CType(vbLf, Char)}
        TextBox1.Text = doc.Range().Text.Trim(chars)

        ' Close the Document, not saving changes.
        doc.Close(SaveChanges:=False)

        ' Close the Word server.
        word_server.Quit()
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated