Using JavaScript to Open Excel and Word Files in HTML

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>

17 thoughts on “Using JavaScript to Open Excel and Word Files in HTML”

  1. Yordanka Dragieva

    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….

  2. 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

  3. 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

  4. 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

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

  6. 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

    1. 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

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

    L

  8. Hey Can I use ActiveXObject on Linux machine.

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

    Thanks

  9. 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?

  10. 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 are closed.

Scroll to Top