Click here to Skip to main content
15,883,750 members
Articles / Web Development / ASP.NET

Open_Word_Excel_using_JavaScript

Rate me:
Please Sign up or sign in to vote.
4.74/5 (11 votes)
6 Aug 2011CPOL1 min read 50.4K   752   11   3
This article will help you to open Word/Excel files using JavaScript

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

JavaScript
var objExcel;
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = true;
objExcel.Workbooks.Open(//file Path);    

Open Word file

JavaScript
var objDoc;
objDoc = new ActiveXObject("Word.Application");
objDoc.Visible = true;
objDoc.Documents.Open(//file Path); 

Problem Might Occur

While dealing with ActiveXObject, mostly security threat occurs that will lead to the following error.

Word Error

Resolution

To overcome this error, we have to change the browser setting. InternetExplorer --> Tools --> InternetOntions --> Security --> Custom Level.

Select the following option:

IE Settings

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Hi there, I am Prasad. Author, Blogger, contributor and passionate about Microsoft .NET technologies. I like to write an articles/blogs on different .NET aspects and like to help Developers, to resolve their issues and boost them on Microsoft Technologies.


Certifications: Microsoft Certified professional (MCP), Microsoft Certified technology specialist (MCTS), Agile-Scrum Master.


Awards: Microsoft Re-connect MVP (GSC Member), Most valuable member at dotnetspider, Most popular curator, Most active curator, featured curator at Microsoft Curah, Editor at dotnetspider.


Microsoft MVP 2014 [ASP.NET/IIS]
Click here for more .NET Tips
-After all Knowledge is an endless entity

Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 767875812-Oct-11 3:01
Member 767875812-Oct-11 3:01 
Good job Prasad.
GeneralRe: My vote of 3 Pin
koolprasad200314-Nov-11 23:48
professionalkoolprasad200314-Nov-11 23:48 
GeneralGood for the begineers Pin
Niral Soni8-Sep-11 5:25
Niral Soni8-Sep-11 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.