用Microsoft Word实现服务器端拼写检查

ZDNet软件频道 时间:2000-05-31 作者:Jason Fisher |  我要评论()
本文关键词:
该例程讲述如何利用WEB服务器端的Word配合ASP进行对用户输入的文本进行拼写检查的任务。
Listing B: Let the Spell-checking begin!
<%
option explicit

'Dimension object variables
dim objWord 'as Word.Application
dim objDocument 'as Word.Document
dim objErrors 'as Word.ProofreadingErrors
dim objError 'as Word.Range
dim wdSuggestions 'as Word.SpellingSuggestions
dim wdSuggestion 'as Word.SpellingSuggestion
dim strSubmission, strModified, intCounter
const wdDoNotSaveChanges = 0

'Capture submitted text from Form or QueryString
strSubmission = request("txtSubmit")

'Echo submission back to the user
with response
	.write "<font size=5><b>"
	.write "Spelling Results</b></font>"
	.write "<br><br>"
	.write "<b>You submitted the following:</b><br>"
	.write "&quot;" & strSubmission & "&quot;<br><br>"
end with

'Launch Word and create a new document
set objWord = server.createObject("Word.Application")
set objDocument = objWord.documents.add

'Insert the user's submitted text into the document
objWord.selection.typeText cstr(strSubmission)

'Retrieve and display spelling errors
set objErrors = objDocument.spellingErrors
if objErrors.count then 'there are errors
	with response
		.write "<table><tr><td><b>Spelling Error&nbsp;"
		.write "</b></td><td><b>Suggestion</b></td></tr>"
	end with
	for each objError in objErrors
		'Display the misspelled word
		response.write "<tr valign=top><td>"
		response.write objError.Text & "</td><td>"
		'Get spelling suggestions for the word
		set wdSuggestions = _
			objWord.getSpellingSuggestions(objError.text)
		if wdSuggestions.count < 1 then
			response.write "No suggestions"
		else
			intCounter = 0
			for each wdSuggestion in wdSuggestions
				intCounter = intCounter + 1
				strModified = replace(strSubmission, _
					objError.text, wdSuggestion.name, 1, 1)
				with response
					'Construct anchor tag
					.write "<a href=SpellCheck.asp?"
					.write "txtSubmit="
					.write server.URLEncode(strModified)
					.write ">" & wdSuggestion.name & "</a>"
				end with
				'Display a pipe-delimiter except at the end
				if intCounter < wdSuggestions.count then
					response.write " | "
				end if
			next
		end if
		response.write "</td></tr>"
	next
	with response
		'Show the user number of errors found
		.write "</table>"
		.write "<br><b>" & objErrors.count
		.write " spelling error(s) found:</b>"
		.write " Click suggestions to correct."
	end with
else 'no errors were found
	response.write "<b>No spelling errors found.</b>"
end if

'Close running instance of Word and destroy objects
objWord.Quit wdDoNotSaveChanges
set objDocument = Nothing
set objWord = Nothing
set objErrors = Nothing
set wdSuggestions = Nothing
%>

返回


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134