Team Fly |
Page 94
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 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.
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, _ |
Team Fly |