Search code examples
javascriptjqueryexport-to-excel

Getting error while Exporting html table to excel


Im getting the following error while exporting an html table to excel; SCRIPT429: Automation server can't create object... This function was working fine but when i restarted my pc and the the application it stopped working not sure whats going on.

function write_to_excel() {
str = "";
var myTable = document.getElementById('myTable');
var rows = myTable.getElementsByTagName('tr');
var rowCount = myTable.rows.length;
var colCount = myTable.getElementsByTagName("tr")[0]
        .getElementsByTagName("th").length;

var ExcelApp = new ActiveXObject("Excel.Application"); //Debug shows error at this  line
var ExcelWorkbook = ExcelApp.Workbooks.Add();
var ExcelSheet = ExcelWorkbook.ActiveSheet;

ExcelApp.Visible = true;
ExcelSheet.Range("A1", "Z1").Font.Bold = true;
ExcelSheet.Range("A1", "Z1").Font.ColorIndex = 23;

// Format table headers
for ( var i = 0; i < 1; i++) {
    for ( var j = 0; j < colCount - 1; j++) {
        str = myTable.getElementsByTagName("tr")[i]
                .getElementsByTagName("th")[j].innerHTML;
        ExcelSheet.Cells(i + 1, j + 1).Value = str;
    }
    ExcelSheet.Range("A1", "Z1").EntireColumn.AutoFit();
}
for ( var i = 1; i < rowCount; i++) {
    for ( var k = 0; k < colCount - 1; k++) {
        str = rows[i].getElementsByTagName('td')[k].innerHTML;
    ExcelSheet.Cells(i + 1, k + 1).Value =  myTable.rows[i].cells[k].innerText;
    }
    ExcelSheet.Range("A" + i, "Z" + i).WrapText = true;
    ExcelSheet.Range("A" + 1, "Z" + i).EntireColumn.AutoFit();
}

return;
  }

Solution

  • Automation server can't create object.

    Means your ActiveX settings are too high so the code will not run.