Introduction
Many times, we need to open files on Client side. Scripting languages played a strong role in such activities. JavaScript is one of the most powerful scripting languages which will allow you to open any file on client side. Window.Open(//File Path)
method will help you to open any file.
Why to Use Client Side Automation
Though javascript:window.open()
will let you open files, files are open in Browser. To avoid this, client side automation of Word and Excel works.
Getting Started
Define a JavaScript function on ASPX page and pass file path to that function.
Open Excel file
var objExcel;
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = true;
objExcel.Workbooks.Open();
Open Word file
var objDoc;
objDoc = new ActiveXObject("Word.Application");
objDoc.Visible = true;
objDoc.Documents.Open();
Problem Might Occur
While dealing with ActiveXObject
, mostly security threat occurs that will lead to the following error.
Resolution
To overcome this error, we have to change the browser setting. InternetExplorer --> Tools --> InternetOntions --> Security --> Custom Level.
Select the following option:
Limitation
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.
Points of Interest
Client side automation is a good alternative to server side automation. Server side automation is not recommended by Microsoft itself. Here is the post.
History
- 6th August, 2011: Initial version