HOWTO: Server-Side Spell Checking Using Microsoft Word and ASP

ID: Q214545


The information in this article applies to:
  • Microsoft Word 97 for Windows
  • Microsoft Visual InterDev, version 6.0
  • Microsoft Internet Information Server version 4.0


SUMMARY

This article demonstrates how you can use Microsoft Word to add spelling check functionality to your Web pages using ASP.


MORE INFORMATION

Follow the steps below to build the ASP application:

  1. On your Web server machine, start Microsoft Visual Interdev 6.0, and choose File/New Project.


  2. In the New Project dialog box, type "WebSpell" in the Name edit box and then double-click the New Web Project icon.


  3. Type or select your Web server name in the Web Project Wizard dialog box that comes up. Leave the working mode as Master, click Next, and then click Finish on the following dialog box.


  4. After Visual InterDev finishes creating your project, open the Project menu and select Add Web Item\HTML Page. Name it "CheckSpelling," and then click Open.


  5. The HTML page you added opens in Design-view by default. Drag an HTML Text Area and an HTML Submit button onto the page. Arrange them any way you like, and type some text into the page informing the user to input their text to be spell checked into the text area.


  6. Select everything on your page (CTRL+A), then choose Form from the Visual InterDev HTML menu. This will wrap your controls in an HTML Form.


  7. Click on the Source tab at the bottom of the page to switch to Source view. Change the HTML opening <FORM> tag's action property to results.asp.


  8. Open the Project menu, and select Add Web Item\Active Server Page. Name it "results," and then click Open.


  9. Switch to Source view for the new page, and put the following code between the <BODY> tags:
    
    <!-- Page header -->
    <p><center><font size=+4 color=red>Spelling Results</font></center><hr>
    <!-- Show user the text they entered -->
    <p>The text you entered was:<p>
    <font color=blue><%=Request("TEXTAREA1")%></font><p><hr><p>
    <!-- Begin server-side script to check spelling errors -->
    <%
       ' Don't allow other sessions to re-enter :)
       do while(Application("WordInUse") = 1)
       loop
       Application("WordInUse") = 1
      
       ' Get Word references created in global.asa.
       dim wdApp
       set wdApp = Application("WordApp")
       dim wdDoc
       set wdDoc = Application("WordDoc")
    
       ' Clear current contents.
       dim wdRange
       set wdRange = wdApp.Selection.Range
       wdRange.WholeStory
       wdRange.Delete
       set wdRange = Nothing
    
       ' Add the text the web user entered.
       dim txt
       txt = Request("TEXTAREA1")
       wdApp.Selection.TypeText CStr(txt)
       
       ' Check spelling without prompting.
       'wdDoc.CheckSpelling , , 0
    
       ' Get spelling errors collection.
       dim wdErrors
       set wdErrors = wdDoc.SpellingErrors 
    %>
    
    <% ' Handle no-error condition.
       if wdErrors.Count = 0 then
    %>
    There were no spelling errors.
    <%
       ' Otherwise build a table of suggestions.
       else
    %>
    <!-- Build a table to show errors & suggestions -->
    <font color=red>There were <%=wdErrors.Count%> spelling error(s).</font><p>
    <TABLE border=1 cellPadding=1 cellSpacing=1 width=75%>
       <TR>
          <TD><b><font size=+1>Word</font></b></TD>
          <TD><b><font size=+1>Suggestions</font></b></TD></TR>
    <%
          for each wdError in wdErrors
             ' Write the word in question.
             Response.Write("<TR><TD>")
             Response.Write(wdError.Text)
             Response.Write("</TD><TD>")
    
             ' Get spelling suggestions for it.
             dim wdSuggestions
             set wdSuggestions = wdApp.GetSpellingSuggestions(wdError.Text)
          
             if wdSuggestions.Count <> 0 then 
                ' a comma-separated list of suggestions.
                dim strSuggestions
                strSuggestions = ", "
                for each wdSuggestion in wdSuggestions
                   strSuggestions = strSuggestions & wdSuggestion.Name & ", "
                next
    
                ' Remove extra comma & space.
                strSuggestions = Right(strSuggestions, len(strSuggestions)-2)
    
                ' Write out suggestions.
                Response.Write(strSuggestions)
             else
                Response.Write("None.")
             end if
             set wdSuggestions = Nothing
             Response.Write("</TD></TR>")
          next
    
       end if
    
       ' Release references.
       set wdErrors = nothing
       set wdDoc = nothing  
       set wdApp = nothing
    
       ' We're done, allow other sessions to continue.
       Application("WordInUse") = 0   
    %> 


  10. In your Visual InterDev Project Explorer, double-click your Global.asa file and add the following two sub-routines between the <SCRIPT> tags:
    
    Sub Application_OnStart()
    
       ' Launch Word.
       dim wdApp
       set wdApp = CreateObject("Word.Application")
       set Application("WordApp") = wdApp   
        
       ' Add a document.
       set Application("WordDoc") = wdApp.Documents.Add
       
       ' Release reference.
       set wdApp = nothing
    
    End Sub
    
    Sub Application_OnEnd()
    
       ' Get Automation references.
       dim wdApp
       set wdApp = Application("WordApp")
       dim wdDoc
       set wdDoc = Application("WordDoc")
       
       ' Tell Word to shutdown.
       wdDoc.Saved = true
       wdApp.Quit
       
       ' Release references.
       set Application("WordDoc") = Nothing
       set Application("WordApp") = Nothing
       set wdDoc = nothing
       set wdApp = nothing
    
    End Sub 


  11. Finally, right-click the CheckSpelling.htm file in the Project Explorer, and choose Set As Start Page.


  12. Choose Save All from the File menu (CTRL+SHIFT+S), then choose Build from the Build menu (or Control-Shift+B).



Now you are ready to test it out. Navigate to your Web site on a client machine, such as http://<servername>/WebSpell/CheckSpelling.htm.

Type in some text in the Web page text area, then click Submit. You should see the results.asp page come up reporting the text you entered and any spelling errors and suggestions it finds.

The project works as follows:

When a user first navigates to the CheckSpelling.htm page, the Application_OnStart() is triggered. This procedure launches Microsoft Word, prepares it for your spell checking use, and stores its Application and Document objects in two ASP Application level variables. This makes the pages more efficient, because you will reuse the same instance of Word instead of invoking multiple instances for each spell checking request. Next, when the user clicks the Submit button, your results.asp page captures the input via the ASP Request object, then uses the stored Microsoft Word objects to perform the spell checking. The results.asp page takes care of problems that could arise where multiple user sessions try to use the same instance at the same time, by blocking each request if another is already in progress.

NOTE: Once a Web user has entered your project, the Web server will have a WINWORD.EXE process running in the background that is handling the spell checking requests. The ASP Application only releases this instance during an Application_OnEnd() event, which is usually only triggered when the Web service is shutdown. You can shutdown and restart your Web service by running the following commands:


net stop w3svc
net start w3svc 

Also, because of a bug in Microsoft Word 97, you won't be able to use Word on the Web server as the logged-on user while the ASP Application is using Microsoft Word. The symptom of this bug is that when you try to launch Microsoft Word, nothing happens.

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Joe Crump, Microsoft Corporation

Additional query words: getspellingsuggestions iis pws grammar

Keywords : kbole kbASP kbASPObj kbAutomation kbInternet kbVBScript kbVisID kbWord kbGrpDSO kbInetDev
Version : WINDOWS:6.0,97; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: June 17, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.