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 """ & strSubmission & ""<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 "
.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
%>
返回