PDA

View Full Version : Spell Checking


Archangel
08-31-2005, 02:14 PM
Okay, I have this spell checking script that I've ganked and improved upon which opens Word in the background to do the spell checking. It works pretty well but I'm having two problems. It's still usable, but these are annoyances.

1. When the correction window is closed the screen flashes real quick
2. I can LTrim with the while loop I have and I can RTrim with the other. But when I try LTrimming AND RTrimming it doesn't work.

/*******************************************************************************************
BEGIN FULL PAGE SPELL CHECK
*******************************************************************************************/
// fso just so spell check works 1st time (focus issue)
var fso
fso = new ActiveXObject("Scripting.FileSystemObject")

//Begin Function
function Full_Spell_Check() {

var objWord, TrimmedString, SecondString

//Create Word ActiveXObject
objWord = new ActiveXObject("Word.Application")
objWord.visible = false

//Open Word Document
objWord.visible = false
objWord.Documents.Add()

//Check all forms on the page
for (f = 0; f < document.forms.length; f++) {

//Check all elements in the form
for (i = 0; i < document.forms[f].elements.length; i++) {

//for ( var x = TrimmedString.length - 1; TrimmedString.charAt(x) <= " "; x-- ) {
// return TrimmedString.substring( 0, x + 1);
//)

//If the elements are text boxes or textareas then check their spelling (as long as they are filled in)
if (((document.forms[f].elements[i].type == 'text') || (document.forms[f].elements[i].type == 'textarea')) && document.forms[f].elements[i].value != '') {

//Set focus to the current form field
document.forms[f].elements[i].focus();
document.forms[f].elements[i].select();

//Set Variable equal to the current fields value
TrimmedString = document.forms[f].elements[i].value;


//Trim the right side of the string
while (TrimmedString.substring(0,1) == ' ')
{
TrimmedString = TrimmedString.substring(1, TrimmedString.length);
}

//Trim left side of the string. Can't get both trims to work together.
//while (SecondString.substring(SecondString.length-1, SecondString.length) == ' ')
//{
// TrimmedString = TrimmedString.substring(0,TrimmedString.length-1);
//}


try {

var str, CTR

//Copy the form fields value to the clipboard
document.execCommand("Copy")

//Do more clipboard stuff
str = window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,"")
CTR = document.selection.createRange()
CTR.findText(TrimmedString)
CTR.select()
document.execCommand("Copy")

//Make sure window stays invisible
objWord.visible = false

//Add selected text to word document and check grammer and spelling
objWord.Selection.Paste()

//objWord.ActiveDocument.CheckGrammar()
objWord.ActiveDocument.CheckSpelling()
objWord.Selection.WholeStory()
objWord.Selection.Copy()

//Copy text back to the clipboard
window.clipboardData.setData('text', window.clipboardData.getData("Text").replace(/^\s+/,"").replace(/\s+$/,""))

//Put text into the field it came from
CTR.text = window.clipboardData.getData("Text")

//Clear the clipboard
window.clipboardData.clearData();

} catch(e) {
alert(e + '\n\nThis error occurred while\nattempting to load MS Word')
}



}//End Try
}//End If
}//End For
//Close word document
objWord.ActiveDocument.Close(0)

//Quit word and destroy the object
objWord.Quit()
objWord = null

//Let the user know the spell check is complete
alert("Spell Check Complete")
}//End For
/*******************************************************************************************
END FULL PAGE SPELL CHECK
*******************************************************************************************/