使用ASP和Word進行服務器端拼寫檢查 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 使用ASP和Word進行服務器端拼寫檢查 (http://www.webasp.net/article/9/8517.htm) |
| -- 作者:未知 -- 發佈日期: 2004-04-01 |
| 使用ASP和Word進行服務器端拼寫檢查
本文討論的問題與下列方面相關: 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、對於新頁面,切換到源碼視圖,在<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注意了當多個用戶會話同時使 用同一實例時可能發生的問題,如果一個用戶正在使用,就進行調度處理。 注意:一旦一個Web用戶登錄了工程文件,Web服務器就會有一個WinWord.exe進程在後台運行,它將處理拼寫檢查的請求。 當應用程序發生OnEnd()事件時,ASP應用程序才會釋放這個實例,而OnEnd()事件只有當Web服務停止時才被觸發。可 以通過運行下列的命令來停止並重新啟動Web服務: net stop w3svc net start w3svc |
| webasp.net |