
Access your |
![]() |
by Eddie Mijares |
Misspelled words seem to stand out on documents and ruin the entire message.
In the last few years, virtually all word processors which hit the market contain a spell checking feature. Most fax and personal information manager packages also have some form of spell checking. Database products have been slow to provide any spelling features. The Windows 95 version of Microsoft Access includes a spell checking feature. Many of my customers want spell checking but are delaying moving to Windows 95 or Windows NT. Luckily, we can use OLE (Object Linking and Embedding) to access the spelling feature in Microsoft Word.
OLE is perhaps the most feared beast in the universe. Most developers will make a quick dash for the door when OLE is even mentioned. The reasons for this fear are misunderstanding and lack of knowledge. OLE is simply the collaboration of one product with another. When dealing with another product we must use the syntax and language of that product. This is where most confusion occurs. If we want to interact with Word from Access then we must use Word functions and methods to get Word to respond.
Let us begin with a new form which contains a text box and a command button. Name the text box `txtSpell' and the command button `cmdSpell'. Now insert the following code:
Sub cmdSpell_Click ()
Dim objWordBasic As Object
Dim TempString$
Dim work$
On Error Resume Next
Err = 0
Set objWordBasic = CreateObject("Word.Basic")
If Err > 0 Then
MsgBox "Could not Load Microsoft Word!"
Exit Sub
End If
work$ = [txtSpell]
objWordBasic.FileNew
objWordBasic.Insert work$
objWordBasic.ToolsSpelling
objWordBasic.EditSelectAll
objWordBasic.SetDocumentVar "MyVar", objWordBasic.Selection
TempString$ = objWordBasic.GetDocumentVar("MyVar")
` remove Carriage Return (13) at end of string
[txtSpell] = Left(TempString$, Len(TempString$) - 1)
End Sub
The first part of the code identifies any variables which we will be using in our code. The next thing we should do is tell error checking to go to the next statement if any errors occur. Next we will set the Error code to zero to clear out any errors. If Microsoft Word is not on our system or an error occurs when we try to create a Word object then the ERR function will return a value greater then zero. We can then inform the user and exit the code. Otherwise, we can load the text box data into a variable, create a new file in Word and insert the data in the variable into the file. Now we can have Word do the spell checking. After the spell checking is complete, we can select the entire document and assign the document to a Word variable. Then we can have Word return the contents of the Word variable. At this point we have the text after spell checking. One problem remains: Word inserts a Carriage Return or ASCII code 13 at the end of the text. In order to remove the Carriage Return, we can place all of the returned text except the last character into the text box. Now the user has his spelling checked text back in the text box.
OLE between Access and Word should be easier for you once you have this example in your arsenal of tools. Microsoft has gone to great lengths to integrate their products. Unfortunately, they have not done a very good job of educating the development community on the ease and elegance of using OLE as a tool.
I am confident that OLE will become easier and more commonplace in the future. But don't cut and run when asked to do OLE development; there are lots of code samples available for the OLE challenged developer. If you can integrate Word, Excel and Access then the users of your applications will have access to tremendous functionality and features.
Eddie Mijares, a HAL-PC member, who is the president of Major Systems Corp., engaged in MS Access and VB consulting.
E-mail me at webmaster@hal-pc.org with any comments you have and tell me what you want to see here.
Back to the Magazine Home Page