快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

使用ASP和WORD进行服务器端拼写检查

概要
本文描述了如何使用Microsoft Word在Web页面ASP文件中添加拼写检查功能。

具体的步骤
按照下列步骤建立ASP应用程序:

1、在Web服务器所在机器上,启动Microsoft Visual Interdev 6.0,选择File/New Project。

2、在“新工程”对话框的名字编辑域中,输入“WebSpell”,然后双击新Web工程图标。

3、在接着出现的Web工程向导对话框中,输入或者选择你的Web服务器名字。将工作模式默认为Master,点击Next,再点击
“finish”。

4、在Visual InterDev创建工程完成后,打开工程菜单,选择“添加Web ItemHTML页面”,命名为“CheckSpelling”,
然后点击Open。

5、添加的HTML页面默认状态下以设计视图打开。在页面上拖出一个HTML文本区域,放置一个HTML提交按钮,根据你的兴趣
进行布局,在页面上输入一些文字,告诉用户在文本域中输入需要进行拼写检查的文字。

6、选择页面上的所有对象(CTRL+A),然后从Visual InterDev的 HTML菜单中选择Form,将对象包裹在表单中。

7、点击当前窗口底部的源码功能页面,切换到源码显示视图。修改HTML开放< FORM >标记的action属性值为
results.asp。

8、打开Project菜单,选择“添加Web ItemActive Server Page”,命名为“results”,然后点击“Open”。

9、对于新页面,切换到源码视图,在<BODY>标记之间输入下面的代码:

<!-- 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、在Visual InterDev 工程浏览窗口中,双击Global.asa文件,在< SCRIPT >标记之间添加下面2段子程序:

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、最后,在工程浏览窗口中用鼠标右键单击CheckSpelling.htm文件,选择“设置为初始页面”。

12、从File菜单中选择“保存所有”(CTRL+SHIFT+S),再从Build菜单中选择“Build”(Control-Shift+B)。

现在可以进行测试了,在客户端输入“http:///WebSpell/CheckSpelling.htm”。

在Web页面的文本域中输入一些文字,点击“Submit”,然后就可以看到results.asp对你输入的文字报告一些错误拼写和
建议。

工程的工作流程
当用户首次浏览到CheckSpelling.htm页面时,Application_OnStart()事件被触发。这个过程启动Microsoft Word,为拼写检查做预备,保存应用和文档对象到2个ASP应用程序级别的变量中。这使页面变得很有效率,因为你可以再次调用Word的同一实例,而不是为每一次拼写检查要求都执行多次实例。接着,当用户点击按钮Submit时,result.asp页面通过ASP的Request对象获取输入值,然后利用存储的Microsoft Word对象来执行拼写检查。result.asp注重了当多个用户会话同时使用同一实例时可能发生的问题,假如一个用户正在使用,就进行调度处理。
返回类别: 教程
上一教程: ASP中实用的广告交替组件
下一教程: 一个很强的验证脚本

您可以阅读与"使用ASP和WORD进行服务器端拼写检查"相关的教程:
· 用SOAP和ASP进行服务器端更新
· 个人经验:使用ASP尽量减少服务器端的工作量
· asp+语法教程(三)asp+的服务器端编程初步
· 使用XMLDOM在服务器端生成静态HTML页面
· 使用索引服务器 - 创建ASP页面
    微笑服务 优质保证 索取样品