编译 甘冀平(2000-09-26)
本文讨论的问题与下列方面相关:
Microsoft Word 97 for Windows
Microsoft Visual InterDev, version 6.0
Microsoft Internet Information Server version 4.0
概要
本文描述了如何使用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 Item\HTML页面”,命名为“CheckSpelling”,
然后点击Open。
5、添加的HTML页面默认状态下以设计视图打开。在页面上拖出一个HTML文本区域,放置一个HTML提交按钮,根据你的爱好
进行布局,在页面上输入一些文字,告诉用户在文本域中输入需要进行拼写检查的文字。
6、选择页面上的所有对象(CTRL+A),然后从Visual InterDev的 HTML菜单中选择Form,将对象包裹在表单中。
7、点击当前窗口底部的源码功能页面,切换到源码显示视图。修改HTML开放< FORM >标记的action属性值为
results.asp。
8、打开Project菜单,选择“添加Web Item\Active Server Page”,命名为“results”,然后点击“Open”。
9、对于新页面,切换到源码视图,在
1<body>标记之间输入下面的代码:
2
3<!-- Page header -->
4<p><center><font color="red" size="+4">Spelling Results</font></center><hr/>
5<!-- Show user the text they entered -->
6<p>The text you entered was:<p>
7<font color="blue">```
8=Request("TEXTAREA1")
9```</font><p><hr/><p>
10<!-- Begin server-side script to check spelling errors -->
11
' 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
1
2
3
' Handle no-error condition.
if wdErrors.Count = 0 then
1
2There were no spelling errors.
3
' Otherwise build a table of suggestions.
else
1
2<!-- Build a table to show errors & suggestions -->
3<font color="red">There were ```
4=wdErrors.Count
5``` spelling error(s).</font><p>
6<table border="1" cellpadding="1" cellspacing="1" width="75%">
7<tr>
8<td><b><font size="+1">Word</font></b></td>
9<td><b><font size="+1">Suggestions</font></b></td></tr>
10
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
1
210、在Visual InterDev 工程浏览窗口中,双击Global.asa文件,在< SCRIPT >标记之间添加下面2段子程序:
3
4Sub Application_OnStart()
5
6
7
8' Launch Word.
9
10dim wdApp
11
12set wdApp = CreateObject("Word.Application")
13
14set Application("WordApp") = wdApp
15
16
17
18' Add a document.
19
20set Application("WordDoc") = wdApp.Documents.Add
21
22
23
24' Release reference.
25
26set wdApp = nothing
27
28
29
30End Sub
31
32
33
34Sub Application_OnEnd()
35
36
37
38' Get Automation references.
39
40dim wdApp
41
42set wdApp = Application("WordApp")
43
44dim wdDoc
45
46set wdDoc = Application("WordDoc")
47
48
49
50' Tell Word to shutdown.
51
52wdDoc.Saved = true
53
54wdApp.Quit
55
56
57
58' Release references.
59
60set Application("WordDoc") = Nothing
61
62set Application("WordApp") = Nothing
63
64set wdDoc = nothing
65
66set wdApp = nothing
67
68
69
70End Sub
71
7211、最后,在工程浏览窗口中用鼠标右键单击CheckSpelling.htm文件,选择“设置为初始页面”。
73
7412、从File菜单中选择“保存所有”(CTRL+SHIFT+S),再从Build菜单中选择“Build”(Control-Shift+B)。
75
76现在可以进行测试了,在客户端输入“http:///WebSpell/CheckSpelling.htm”。
77
78在Web页面的文本域中输入一些文字,点击“Submit”,然后就可以看到results.asp对你输入的文字报告一些错误拼写和
79建议。
80
81工程的工作流程
82当用户首次浏览到CheckSpelling.htm页面时,Application_OnStart()事件被触发。这个过程启动Microsoft Word,为拼写检查做准备,保存应用和文档对象到2个ASP应用程序级别的变量中。这使页面变得很有效率,因为你可以再次调用Word的同一实例,而不是为每一次拼写检查要求都执行多次实例。接着,当用户点击按钮Submit时,result.asp页面通过ASP的Request对象获取输入值,然后利用存储的Microsoft Word对象来执行拼写检查。result.asp注意了当多个用户会话同时使用同一实例时可能发生的问题,如果一个用户正在使用,就进行调度处理。
83
84注意:一旦一个Web用户登录了工程文件,Web服务器就会有一个WinWord.exe进程在后台运行,它将处理拼写检查的请求。当应用程序发生OnEnd()事件时,ASP应用程序才会释放这个实例,而OnEnd()事件只有当Web服务停止时才被触发。可以通过运行下列的命令来停止并重新启动Web服务:
85
86net stop w3svc
87
88net start w3svc</table></p></p></p></p></p></p></body>