Javascript to open a word document

  • 2020-03-30 02:37:41
  • OfStack

First we create a new HTML file and write a FileUpLoad and a button control.

The code is as follows:


<input id="flUpload" type="file" />fileUpload 
<input id="btnOpenFile" type="button" value="button" onclick="OpenFile()" />

Then, write a javascript OpenFile method.

The code is as follows:


function OpenFile() 
{ 
if (document.getElementById("flUpload").value.toUpperCase().indexOf(".XLS") != -1) 
{ 
var objExcel; 
objExcel = new ActiveXObject("Excel.Application"); 
objExcel.Visible = true; 
objExcel.Workbooks.Open(document.getElementById("flUpload").value); 
} 
else if (document.getElementById("flUpload").value.toUpperCase().indexOf(".DOC") != -1) 
{ 
var objDoc; 
objDoc = new ActiveXObject("Word.Application"); 
objDoc.Visible = true; 
objDoc.Documents.Open(document.getElementById("fileUpload").value); 
} 
else 
{ 
alert("Please select Word/Excel file only"); 
return false; 
} 
}

OK. Then you can select a word document in IE and click open to open the word document successfully.
Is not very simple, seems to be only used in IE.


Related articles: