6

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>
3
  • What are you trying to achieve? Jul 21, 2011 at 17:11
  • Theoretically you could create the correct binary data and present it in such a way that it may be saved correctly, however this is typically done server side so that the correct mime types and data conversion can be performed.
    – zzzzBov
    Jul 21, 2011 at 17:12
  • 2
    see stackoverflow.com/questions/15899883/…
    – Dan
    Jan 30, 2014 at 14:43

7 Answers 7

5

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.

2

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

2
  • 1
    The question you linked actually shows it is possible to download files from the browser without going to the server through data uris. Though browser support may have been a bit worse when it was answered in 2010, but even so. The problem isn't downloading the file, it's generating it without using ActiveX.
    – Aidiakapi
    Jun 18, 2015 at 18:09
  • It is possible. A guy explains here how to do it. effectiveui.com/blog/2015/02/23/… Mar 31, 2017 at 10:16
1

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

0

You can not get this working using client side. Main thing is you need to send headers not as html. So I would suggest you to use server side scripting as Max suggested and preferably use .htaccess file if you are using Apache server to also name these files as .doc.

Lets assume your php file needs to create a .doc file with some passed argument lets say id. So you want file_.doc to point to file.php?id=, try using following rewrite rule so that browser understands by extension too RewriteRule file_(.*).doc file.php?id=$1

0

if you need server side document generation and server is running Java, take a look at this: https://github.com/leonardoanalista/java2word/

0

This is absolutely possible. Googoose is a jQuery plugin that I wrote to handle a lot of the more complicated conversions. It's still fairly new, but there appear to be a few other attempts at this, so you could check those out. Here is the best documentation I've found so far that actually explains this process http://sebsauvage.net/wiki/doku.php?id=word_document_generation. If you're interested check out the examples in Googoose.

-1

sometimes we can not use server side app or activeX to create office document because of phonegap mobile app that uses only client-side javascipt to operate. the only way i found for now is uding word binary file format or OOXML

http://msdn.microsoft.com/en-us/library/hh643138(v=office.12)

some say that its much easier to create RTF file and i agree with them.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.