Using Word s Features

Team Fly 

Page 94

Using Word's Features

Before you can access functionality from an Office application, you must reference its object library. You need a COM wrapper, as they say. For Word, you have to add a reference in your VB.NET project to—as you might guess—the Word object collection:

1. Start a new Windows-style VB.NET project.

2. Choose Project image Add Reference.

3. Click the COM tab in the Add Reference dialog box, then locate the Microsoft Word 10.0 Object Library (your version number may not be 10.0).

4. Double-click this object library, then click OK to close the dialog box and add the reference.

NOTE At the start of the sections on Outlook and Excel below, you'll find instructions on referencing their objects.

The following sections describe how to use various handy features of Microsoft Word in your VB.NET applications.

Spell-Checking

To start things off, let's see how to check spelling in a VB.NET TextBox by borrowing that capability from Word's spell checker. We'll explore three different ways to accomplish this goal:

1. Sending TextBox text into a Word document, employing the spell-check dialog box to interact with the user to fix any spelling problems, then sending the fixed text back to the TextBox after spell-checking, via the Clipboard.

2. Feeding text directly into the Word spell-check utility and getting back an all or nothing (yes or no) answer as to whether there were any spelling errors in the string.

3. Same as number 2, except in this case getting back a list of misspelled words, and lists of alternative spelling suggestions for each misspelled word. With this approach, you can construct your own VB.NET version of the Word spell-check dialog.

To try the first example (Listing 4.1), start a new Windows-style VB.NET project and add a TextBox and a Button to Form1, then type this into the Button's Click event.

LISTING 4.1: SPELL-CHECKING A VB.NET TEXTBOX

Private Sub Button1_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Button1.Click

        Dim w As Object = New Word.Application 'instantiate a Word app
        w.visible = False 'don't confuse things by showing the app

        Dim d As Object = w.Documents.Add 'create a word document
        Dim id As IDataObject 'will contain the results sent back from the clipboard

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net