Using JavaScript to Open Excel and Word Files in HTML

by Yang Yang on January 5, 2009

One of the main reasons that you might need this is that Microsoft Excel and Word documents pervade. They are perfect media to store, display and distribute information and ideas.

A rather straightforward approach is to simply link that document up.

<a href="bank/sheet.xls" >some excel file</a>

Another approach is via JavaScript.

<script type="text/javascript">
	function openExcel(strFilePath) {
		var yourSite = "http://www.yoursite.com";
		openExcelDocPath(yourSite + strFilePath, false);
	}
	function openExcelDocPath(strLocation, boolReadOnly) {
		var objExcel;
		objExcel = new ActiveXObject("Excel.Application");
		objExcel.Visible = true;
		objExcel.Workbooks.Open(strLocation, false, boolReadOnly);
	}
</script>

To open a word document with javascript, go with the following code:

<script type="text/javascript">
	function openWord(strFilePath) {
		var yourSite = "http://www.yoursite.com";
		openWordDocPath(yourSite + strFilePath);
	}
	function openWordDocPath(strLocation) {
		var objWord;
		objWord = new ActiveXObject("Word.Application");
		objWord.Visible = true;
		objWord.Documents.Open(strLocation);
	}
</script>
Subscribe to Kavoir: blog feed

You should also read:

Yordanka Dragieva August 17, 2009 at 9:26 pm

Hi there, very helpful post!
I have a question – what does the 2nd argument in excelApp.Workbooks.Open(Location, FALSE, false) stand for? I understand the 1st and the 3rd one but the second one is not very clear – and I do not seem to find any useful details on msdn….

Raja Chandramohan September 2, 2009 at 4:55 pm

Hi Friend,

Very much thanks to you.

Thanks.
Raja.

Pankaj Saha December 22, 2009 at 1:53 pm

I want to open the .doc file and I have used your above code, but when I run the application its show the following error

Microsoft jscript runtime error : Automatic server can’t create object

any suggestion

ap December 15, 2012 at 12:19 am

You need to change your security settings, just enable all your active x components

elahe August 7, 2010 at 8:11 pm

I want to open the .doc file and I have used your above code, but when I run the application its show the following error

Microsoft jscript runtime error : Automatic server can’t create object

any suggestion

Sekhar February 21, 2011 at 10:59 pm

to resolve your problem, enable the ActiveXObject by going to internetOpt-> Security-> custom level

Sekhar February 21, 2011 at 11:01 pm

i used the same code it is working fine in IE7 but it is not working in Mozilla can you give a solution to it. i try to get this using Open office plugin still i am unable to open

J March 9, 2011 at 8:46 am

@Sekhar ActiveX plugins only work in IE. This code won’t work in Mozilla.

gaurav August 19, 2011 at 11:11 am

hi all.. can you please tell me the alternative to activexobject used in above code.. if i have to use it in other browsers..?

Vaibhav November 18, 2011 at 2:06 am

Can you send me how to fetch data from excel sheet into drop down using JavaScript. Its really very very very very urgent

thanks in advance

Deepak Rosario January 24, 2012 at 2:16 pm

You need to use XMLHTTP instead of ActiveXobject.

Vinay July 27, 2012 at 10:06 am

Hi Deepak

Can u please post the code for this. How to use XMLHTTP instead of ActiveX.
I want to display the excel on to the browser using iframe. Can u please help me out.

Thanks

Vinay July 27, 2012 at 10:07 am

If possible on share the code…

larry May 11, 2012 at 5:29 am

I use this code It invokes an instance of Word but does not open my doc. Whas up with that?

L

Rayn July 25, 2012 at 3:24 pm

Hey Can I use ActiveXObject on Linux machine.

Is there another way to to create object on linux machine except ActiveXObject.

Thanks

qwrrtty October 15, 2012 at 5:39 pm

HI this works fine to open an xls file but is not working if i use it to open an xlsm file. ANy ideas on how to do that?

idan January 26, 2013 at 5:52 am

How can I enhance it to check if file is already open before opening it?
and how can i interact with an open excel app?

Comments on this entry are closed.

Previous post:

Next post: