Using JavaScript to "create" a Microsoft Word document

I would like to dynamically create a document using JavaScript and then open that document in a Microsoft word. Is it 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> 
+6
source share
7 answers

I'm not sure exactly what you are trying to do in your code, but here is some information I found about accessing a text document and a table in a document:

  • Microsoft Word Object Model

    This object model is part of Microsoft Word (and not Javascript) and allows you to "automate" the word remotely from other programs (not only web pages, but also any computer program).

    It is primarily intended for Visual Basic, but it can be accessed using Javascript from a web page - see paragraph 2 below.

    However, using Javascript is a bit more complicated, especially because you cannot use visual base constants - you need to reference them by value. If you explore this further, you will soon understand what I mean.

    So where can you find out about this object model?

    That's all there is in the Word help files if you are looking for it.

    If you look in the Word help system, in the programming information section you will find a link to Microsoft Word Visual Basic programming.

    The Word object model that allows you to do what you need to solve your problem, for example:

    • Open word
    • Open document in Word
    • Access the table collection in this ActiveDocument.
    • Access to the rows and cells of this table.
  • How do you access this from Javascript?

    This can only be done through Internet Explorer (and possibly in Opera).

    Here you need to learn about ActiveXObjects.

    ActiveXObjects (if you don't know) are separate computer programs that provide additional functionality. There are many ActiveX objects on the Internet.

    When you install Word, it also installs an ActiveX object to automate the word, giving you access to the Word object model.

    So, in javascript, open a new instance of the 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 the 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.

+4
source

I do not believe that this idea will work. You need to create a Word file using the server language. For example, PHP: http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php

+1
source

EDIT: This was not possible when the question was asked, but in 2017. Link to comment from jrm - http://www.effectiveui.com/blog/2015/02/23/generating-a-downloadable-word-document-in-the-browser/

The browser places some serious restrictions on Javascript, which will prevent the creation of a downloadable file. See this related question:

Create a file in memory to be downloaded by the user, not through the server

+1
source

You cannot make this work work on the client side. Most importantly, you need to send the headers not as html. Therefore, I suggest you use server-side scripts, as Max suggested, and it is preferable to use the .htaccess file if you are using the Apache server to also name these files as .doc.

Suppose your php file has to create a .doc file with some argument passed, let's say id. So you want file_.doc to point to file.php? Id =, try using the following rewrite rule so that the browser also understands the RewriteRule file _ (. *) Extension. Doc file.php? Id = $ 1

0
source

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

0
source

sometimes we cannot use a server-side application or activeX to create an office document because of a mobile application for mobile phones that uses only javascipt on the client side. the only way i have found so far is the uding word or OOXML binary file format

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

some say that it’s much easier to create an RTF file, and I agree with them.

-1
source

It is absolutely possible. Googoose is a jQuery plugin that I wrote to handle many more complex transformations. It is still fairly new, but there seem to be a few other attempts, so you can check them out. Here is the best documentation I have found so far which actually explains this process http://sebsauvage.net/wiki/doku.php?id=word_document_generation . If you are interested, see examples in Googoose.

-1
source

Source: https://habr.com/ru/post/893293/


All Articles