18.01.2013 Views

Mastering Visual Basic .NET

Mastering Visual Basic .NET

Mastering Visual Basic .NET

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

450<br />

Chapter 10 AUTOMATING MICROSOFT OFFICE APPLICATIONS<br />

The application will contact Word and request the list of misspelled words. The list of misspelled<br />

words is displayed on a ListBox control on the same Form, as shown in Figure 10.2. The<br />

ListBox control on the left shows all the misspelled words returned by Word. Word can not only<br />

locate misspelled words but suggest alternatives as well. To view the alternate spellings for a specific<br />

word, select the word in the left list by double-clicking it.<br />

To replace all instances of the selected misspelled word with the selected alternative, click the<br />

Replace button. You can design your own interface to allow the user to select which and how many<br />

instances of the misspelled word in the original document will be replaced.<br />

The program uses three public variables, which are declared as follows:<br />

Public WordApp As Application<br />

Public CorrectionsCollection As SpellingSuggestions<br />

Public SpellCollection As ProofreadingErrors<br />

The SpellCollection variable is a collection that contains all the misspelled words, and the<br />

CorrectionsCollection variable is another collection that contains the suggested spellings for a specific<br />

word. The CorrectionsCollection variable’s contents are changed every time the user selects another misspelled<br />

word in Word’s Spelling Suggestions window.<br />

When the SpellCheck Document button is clicked, the program creates a new document and<br />

copies the TextBox control’s contents to the new document using the InsertAfter method of the<br />

Range object, as follows:<br />

WordApp.Documents.Add<br />

Dim Drange As Word.Range<br />

Drange = WordApp.ActiveDocument.Range<br />

DRange.InsertAfter(Text1.Text)<br />

Now comes the interesting part. The <strong>Visual</strong> <strong>Basic</strong> code calls the Range object’s SpellingErrors<br />

method, which returns a collection of ProofreadingErrors objects. The result of the SpellingErrors<br />

method is assigned to the object variable SpellCollection:<br />

Dim SpellCollection As Word.ProofreadingErrors<br />

Set SpellCollection = DRange.SpellingErrors<br />

The lines in Listing 10.5—the SpellCheck Document button’s Click event handler—spell-check<br />

the document and add the words contained in the SpellCollection collection (the misspelled words) to<br />

the ListBox1 control.<br />

Listing 10.5: The Check Document Button<br />

Private Sub Button1_Click(ByVal sender As System.Object, _<br />

ByVal e As System.EventArgs) Handles Button1.Click<br />

Dim DRange As Word.Range<br />

Me.Text = “Starting Word ...”<br />

WordApp.Documents.Add()<br />

Me.Text = “Checking words...”<br />

DRange = WordApp.ActiveDocument.Range<br />

DRange.InsertAfter(TextBox1.Text)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!