试图从运行javascript的html应用程序调用excel宏

时间:2015-09-04 18:32:42

标签: javascript excel vba excel-vba

我编写了一个应用程序来读取用户输入并将所述输入写入.txt文件,因为excel宏更容易读取。我已经编写了一个从文本文档中读取的宏,并将该数据推送到excel工作表。有没有什么可靠的方法从javascript运行excel宏?

以下是我的尝试:

    function RunMacro(objType) {
    switch (objType) {
        case "90":
            var fileName = "P:\\Engineering\\Generated Part Logs\\90-.xlsm";
            break;
        case "69":
            fileName = "P:\\Engineering\\Generated Part Logs\\69-.xlsm";
            break;
        case "66":
            fileName = "P:\\Engineering\\Generated Part Logs\\66-.xlsm";
            break;
        case "40":
            fileName = "P:\\Engineering\\Generated Part Logs\\40-.xlsm";
            break;
        case "30":
            fileName = "P:\\Engineering\\Generated Part Logs\\30-.xlsm";
            break;
        case "09":
            fileName = "P:\\Engineering\\Generated Part Logs\\09-.xlsm";
            break;
        case "02":
            fileName = "P:\\Engineering\\Generated Part Logs\\02-.xlsm";
            break;
    }
    var bookName = fileName.substring(35);
    alert(bookName);
    var Excel = new ActiveXObject("Excel.Application");
    Excel.Workbooks.Open(fileName);
    Excel.Visible = true;
    Excel.Run("Sheet1.ImportPartData1");
}

我在这里正确命名宏吗?或者这是从javascript应用程序访问excel宏的错误方法吗?

2 个答案:

答案 0 :(得分:0)

您需要包含文件名:

Excel.Run("'fileNameHere.xlsm'!Sheet1.ImportPartData1")

答案 1 :(得分:0)

以下是代码:



var Excel = new ActiveXObject("Excel.Application");
Excel.Workbooks.Open("P:\\Engineering\\Generated Part Logs\\90.xlsm");
Excel.Visible = false;
Excel.Run("Sheet1.ImportPartData1");