使用ASP與Word進(jìn)行服務(wù)器端拼寫檢查
發(fā)表時(shí)間:2023-07-29 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]編譯 甘冀平(2000-09-26) 本文討論的問(wèn)題與下列方面相關(guān): Microsoft Word 97 for Windows Microsoft Visual InterDev, version...
編譯 甘冀平(2000-09-26)
本文討論的問(wèn)題與下列方面相關(guān):
Microsoft Word 97 for Windows
Microsoft Visual InterDev, version 6.0
Microsoft Internet Information Server version 4.0
概要
本文描述了如何使用Microsoft Word在Web頁(yè)面ASP文件中添加拼寫檢查功能。
詳細(xì)的步驟
按照下列步驟建立ASP應(yīng)用程序:
1、在Web服務(wù)器所在機(jī)器上,啟動(dòng)Microsoft Visual Interdev 6.0,選擇File/New Project。
2、在“新工程”對(duì)話框的名字編輯域中,輸入“WebSpell”,然后雙擊新Web工程圖標(biāo)。
3、在接著出現(xiàn)的Web工程向?qū)?duì)話框中,輸入或者選擇你的Web服務(wù)器名字。將工作模式默認(rèn)為Master,點(diǎn)擊Next,再點(diǎn)擊
“finish”。
4、在Visual InterDev創(chuàng)建工程完成后,打開(kāi)工程菜單,選擇“添加Web Item\HTML頁(yè)面”,命名為“CheckSpelling”,
然后點(diǎn)擊Open。
5、添加的HTML頁(yè)面默認(rèn)狀態(tài)下以設(shè)計(jì)視圖打開(kāi)。在頁(yè)面上拖出一個(gè)HTML文本區(qū)域,放置一個(gè)HTML提交按鈕,根據(jù)你的愛(ài)好
進(jìn)行布局,在頁(yè)面上輸入一些文字,告訴用戶在文本域中輸入需要進(jìn)行拼寫檢查的文字。
6、選擇頁(yè)面上的所有對(duì)象(CTRL+A),然后從Visual InterDev的 HTML菜單中選擇Form,將對(duì)象包裹在表單中。
7、點(diǎn)擊當(dāng)前窗口底部的源碼功能頁(yè)面,切換到源碼顯示視圖。修改HTML開(kāi)放< FORM >標(biāo)記的action屬性值為
results.asp。
8、打開(kāi)Project菜單,選擇“添加Web Item\Active Server Page”,命名為“results”,然后點(diǎn)擊“Open”。
9、對(duì)于新頁(yè)面,切換到源碼視圖,在<BODY>標(biāo)記之間輸入下面的代碼:
<!-- 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 >標(biāo)記之間添加下面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、最后,在工程瀏覽窗口中用鼠標(biāo)右鍵單擊CheckSpelling.htm文件,選擇“設(shè)置為初始頁(yè)面”。
12、從File菜單中選擇“保存所有”(CTRL+SHIFT+S),再?gòu)腂uild菜單中選擇“Build”(Control-Shift+B)。
現(xiàn)在可以進(jìn)行測(cè)試了,在客戶端輸入“http:///WebSpell/CheckSpelling.htm”。
在Web頁(yè)面的文本域中輸入一些文字,點(diǎn)擊“Submit”,然后就可以看到results.asp對(duì)你輸入的文字報(bào)告一些錯(cuò)誤拼寫和
建議。
工程的工作流程
當(dāng)用戶首次瀏覽到CheckSpelling.htm頁(yè)面時(shí),Application_OnStart()事件被觸發(fā)。這個(gè)過(guò)程啟動(dòng)Microsoft Word,為拼寫檢查做準(zhǔn)備,保存應(yīng)用和文檔對(duì)象到2個(gè)ASP應(yīng)用程序級(jí)別的變量中。這使頁(yè)面變得很有效率,因?yàn)槟憧梢栽俅握{(diào)用Word的同一實(shí)例,而不是為每一次拼寫檢查要求都執(zhí)行多次實(shí)例。接著,當(dāng)用戶點(diǎn)擊按鈕Submit時(shí),result.asp頁(yè)面通過(guò)ASP的Request對(duì)象獲取輸入值,然后利用存儲(chǔ)的Microsoft Word對(duì)象來(lái)執(zhí)行拼寫檢查。result.asp注意了當(dāng)多個(gè)用戶會(huì)話同時(shí)使用同一實(shí)例時(shí)可能發(fā)生的問(wèn)題,如果一個(gè)用戶正在使用,就進(jìn)行調(diào)度處理。
注意:一旦一個(gè)Web用戶登錄了工程文件,Web服務(wù)器就會(huì)有一個(gè)WinWord.exe進(jìn)程在后臺(tái)運(yùn)行,它將處理拼寫檢查的請(qǐng)求。當(dāng)應(yīng)用程序發(fā)生OnEnd()事件時(shí),ASP應(yīng)用程序才會(huì)釋放這個(gè)實(shí)例,而OnEnd()事件只有當(dāng)Web服務(wù)停止時(shí)才被觸發(fā)?梢酝ㄟ^(guò)運(yùn)行下列的命令來(lái)停止并重新啟動(dòng)Web服務(wù):
net stop w3svc
net start w3svc