Jonathan Snook.ca

 

Copying from Microsoft Word to a textarea using JavaScript

October 31, 2001

I did some research and found that not many people had detailed a solution to do this so I've created my own solution. First, there's a couple things that you need to be aware of before you proceed.

  1. This is a client-based solution and therefore your client must have Microsoft Word installed on their machine.
  2. This solution accesses files on the client's machine and therefore your client must have their security settings to allow script not marked as safe to work.

So, let's get right into it:

<html>

<head><title>snook.ca load document</title>

<script language="JavaScript">

<!--//

function loadworddoc(){

  var doc = new ActiveXObject("Word.Application"); // creates the word object

  doc.Visible=false; // doesn't display Word window

  doc.Documents.Open("C:\\My Documents\\file.doc"); // specify path to document

               

  //copy the content from my word document and throw it into my variable

  var txt;

  txt = doc.Documents("C:\\My Documents\\file.doc").Content; 

  document.all.myarea.value = txt;

  doc.quit(0); // quit word (very important or you'll quickly chew up memory!)

}

//-->

</script>

</head>

<body>

  <p><input type=button onClick="loadworddoc();" value="Load">

  <p><textarea name=myarea cols=50 rows=5>nothing here yet</textarea>

</body>

</html>

This is a very quick and dirty solution. There's a lot more that you can do. You'll want to reference the VBA documentation for Word. Do a search on your hard drive for "VBAWRD9.CHM". If it can't be found, you'll likely need to install the documentation off the Microsoft Office CD.

If you have some ideas, feel free to send them to me at ideas@snook.ca.

Added November 1, 2001:

Guess what? I have also included a revised version of this code that includes a browse button so users can search for word documents off their hard drive without needing to know the path. It also copies the code directly into the DHTML Editing Component (DEC).

<html>

    <head><title>snook.ca load document</title>

    <script language="JavaScript">

    <!--//

    function loadworddoc(){

        // creates the word object

        var doc = new ActiveXObject("Word.Application"); 

        // doesn't display Word window

        doc.Visible=false; 

        // specify path to document

        doc.Documents.Open(document.all.hello.value); 

       

       //copy the content from my word document and throw it into my variable

       var txt;

       txt = doc.Documents(document.all.hello.value).Content;

       //document.all.myarea.value = txt;

       document.all.tbContentElement.DOM.body.innerHTML = txt;

       // quit word (very important or you'll quickly chew up memory!)

       doc.quit(0); 

       }

       //-->

       </script>

    </head>

    <body>

       <p><input type=button onClick="loadworddoc();" value="Load">

       <p><input type=file name=hello>

       <p><textarea name=myarea cols=50 rows=5>nothing here yet</textarea>

       <object ID="tbContentElement" CLASS="tbContentElement" 

         CLASSID="clsid:2D360201-FFF5-11D1-8D03-00A0C959BC0A" VIEWASTEXT

         width="450" height="300">

         <param name=Scrollbars value=true></object>

    </body>

</html>

Enjoy!

<< Check out the list of all articles