Showing posts with label text selection. Show all posts
Showing posts with label text selection. Show all posts

Sunday, April 29, 2012

JavaScript code to send text from HTML page to MS Word


<HTML>
 <HEAD>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <SCRIPT type="text/javascript">


function trim(stringToTrim) 
{


return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function SelectionToWord(theField)
{


var tempVal = eval("document." + theField);
tempVal.focus();
tempVal.select();


if(tempVal.createTextRange)
{


theRange = tempVal.createTextRange();

if(trim(theRange.text).length > 0) // If selection is not empty:
{


if(theRange.execCommand)
{


theRange.execCommand("Copy");


// window.status="Contents highlighted and copied to clipboard!";

var word = new ActiveXObject("Word.Application");   // Start MS Word instance:
word.Documents.Add(); // Create new document:
word.Selection.Paste(); // Paste clipboard contents into the document:
word.Visible = true; // Show MS Word:
}


}
else
{


alert("Please type the text and click the button.........");
}


}
else
{


alert("This feature is not available for this Browser.");
}


}
  </SCRIPT>
 </HEAD>


 <BODY>


 <form action="#" name="test2">


 <P>Please type the text and click the button.</P>


<textarea cols='60' rows='2' name='textWord2' id='textWord2' style='font-size:20; font-weight:bold' > </textarea>


<br><br><br>


 <INPUT type="button" onclick="SelectionToWord('test2.textWord2')" value="Send To MS Word"></INPUT>


 </form>


 </BODY>
</HTML>