Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JavaScript to "Create" a Microsoft Word Document

I would like to dynamically create a document using JavaScript and then open that document in Microsoft word. Is this possible? Here is my current code:

<html>
  <head>
      <title></title>

       <script src="js/jquery-1.4.4.js" type="text/javascript"></script>
  </head>
  <body>

  <div id="myDiv">The quick brown fox jumped lazly over the dead log.</div>

  <script type="text/jscript">
     var printWindow = window.open("", "Print", "width=800,height=400,scrollbar=0");
     var printAreaHtml = $("#myDiv").attr("outerHTML");

     printWindow.document.open("text/html", "replace");
     printWindow.document.writeln("<html><head>")
     printWindow.document.writeln("<meta HTTP-EQUIV='Content-Type'  content='application/vnd.ms-word'>");
     printWindow.document.writeln("<meta HTTP-EQUIV='Content-Disposition' content='attachment;filename=print.doc'>");
     printWindow.document.writeln("</head>");
     printWindow.document.writeln("<body>");
     printWindow.document.write(printAreaHtml);

     printWindow.document.writeln("</body>");
     printWindow.document.writeln("</html>");
     printWindow.document.close();

     //    printWindow.print();     

  </script>

  </body>
</html>
like image 312
user535056 Avatar asked Jul 21 '11 17:07

user535056


3 Answers

I'm not sure exactly what you are trying to do in your code up there but here is some information i found about accessing a word document and a table within the doc:

  1. Microsoft Word Object Model

    This object model is part of Microsoft Word (not Javascript) and it lets you "automate" word remotely from other programs (not just web pages, but any computer program).

    It is primarily designed for Visual Basic, but can be accessed by Javascript from a web page - see para 2 below.

    However it is a bit more tricky to use through Javascript, particularly because you cannot use visual basic constants - you need to refer to them by value. If you research this further, you will soon know what I mean by this.

    So where can you find out about this Object Model?

    It is all there in the Word help files if you look for it.

    If you look in the Word help, under programming information, you will find the Microsoft Word Visual Basic Programming Reference.

    The Word object model, which lets you do things you will need to solve your problem like:

    • Open Word
    • Open a Document in Word
    • Access the collection of Tables in that ActiveDocument.
    • Access the Rows and Cells of a given Table.
  2. How do you access this from Javascript?

    This might only be done I think through Internet Explorer (and perhaps Opera).

    Here you need to learn about ActiveXObjects.

    ActiveXObjects (if you do not know) are separate computer programs which enable additional functionality. There are lots of ActiveX objects on the internet.

    When you install Word, this also installs an ActiveX object for automating word, giving you access to the Word Object Model.

    So in javascript, lets open up a new instance of word:

    var oApplication=new ActiveXObject("Word.Application");
    oApplication.Visible=true; // "Visible" is in the Word Object Model`
    

    There you have it.

    Then if you want to open you file and get the table:

    oApplication.Documents.Open("myfilename");
    var oDocument=oApplication.ActiveDocument;
    var oTable=oDocument.Tables(1);`
    

And now I leave it to you to keep going with the rest.

like image 165
Evan Avatar answered Nov 03 '22 05:11

Evan


EDIT: this wasn't possible when the question was asked but in 2017 it is. See link from comment by jrm - http://www.effectiveui.com/blog/2015/02/23/generating-a-downloadable-word-document-in-the-browser/

Browser place some serious restrictions on Javascript which will prevent you creating a downloadable file. See this related question:

Create a file in memory for user to download, not through server

like image 30
Colin Pickard Avatar answered Nov 03 '22 05:11

Colin Pickard


I don't believe that this idea will work. You need to create the Word file with a serverside language. For example PHP: http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php

like image 32
binarious Avatar answered Nov 03 '22 06:11

binarious