Open Word and Excel files using Javascript


Many times, we need to open files on Client side. Scripting languages played a strong role in such activities. here we used word and excel automation to open file using javascript. This is called client side automation technique.

Open Word and Excel files using Javascript



Background


Background behind this article is bit interesting, we have a project in ASP.NET, which open word and excel files on client machine.
and then user can make changes in it. Team members thought, can't we open file using Javascript ? and the answer was "yes, we can but we need some code to try"
and after some trial and error we got small piece of code in javascript, that will open word and excel file in javascript with automation object.
Javascript is one of the most powerful scripting language that allows you to create word and excel object.

why not window.open() ?


window.open() in javascript will let you open word and excel file, but it's open in browser not it in's own editor.
to avoid this we have to use automation with javascript

Let's cook


Write a function in javascript and call it on button click in asp.net.
for e.g. in following code i have take "open file" button and call function javascript function openFile() on it.

code



Here we used ActiveX to create word and excel object

Write a javascript function to open file : WINWORD


Check following javascript function. to create word object

word object



Write a javascript function to open file : EXCEL


Check following javascript function.

excel object



Possible exception


when we deal with ActiveX, most of the time security issue are lift.
and if you run above code may be your browser gives you following error

error


To over come this we have to chnage the browser settings.
we have to tell browser to initialiaze and run activex controls.
change the browser setting. InternetExplorer --> Tools --> InternetOntions --> Security --> Custom Level

Browser setting



Limitations


This article opens Word/Excel using ActiveX object and unfortunately, these AciveX Plugins are only supported by Internet Explorer (tested on IE6/7/8).
These objects are used to create instances of OLE Automation. Several applications (Microsoft Office Word, Microsoft Office Excel, Windows Media Player, ...)
provide OLE Automation objects to allow communication with them.
You can use the methods and properties supported by Automation objects in JavaScript.

Client side automation is a good alternative to server side automation. Server side automation is not recommended by Microsoft itself, check article

Thanking you


Suggestions and Queries are always welcome.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: