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 comments

  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. Raja Chandramohan

    Hi Friend,

    Very much thanks to you.

    Thanks.
    Raja.

  3. Pankaj Saha

    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

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

  4. elahe

    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

  5. Sekhar

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

  6. Sekhar

    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

  7. J

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

  8. gaurav

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

  9. Vaibhav

    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

  10. Deepak Rosario

    You need to use XMLHTTP instead of ActiveXobject.

    • Vinay

      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

        If possible on share the code…

  11. larry

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

    L

  12. Rayn

    Hey Can I use ActiveXObject on Linux machine.

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

    Thanks

  13. qwrrtty

    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?

  14. idan

    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?