0

我正在编写一个网页作为访问 Excel 工作簿的前端,该工作簿为某些硬件生成配置文件。目前这只是我测试这个概念并熟悉 jscript 如何自动化 excel。

我的问题是当我尝试运行宏时,我不断收到“预期的”;第 46 行字符 7 处出错。” 据我所知,语法是正确的,它适用于不同的 Excel 工作簿宏。我已经在我的 PC 上修复了 .dll 并检查了 IE 设置,但让我感到困惑的是为什么这不起作用而其他 jscript 运行得很好。

工作正常:oXL.Run("ButtonTest.xlsm!Module1.buttonclick");

给出错误:oXL.Run("test.xlsm!Module1.makeconfigs");

我的概念测试的完整代码:

<!DOCTYPE html>
<html lang="en">


    <body>
<SCRIPT LANGUAGE="VBScript">

</SCRIPT>

<SCRIPT LANGUAGE="JScript"> 
function AutomateExcel(store,direct,MdfFloor,MdfSW,Include)
{

   // Start Excel and get Application object.
      var oXL = new ActiveXObject("Excel.Application");
      var filename = "D:\\Profiles\\ngwx36\\Desktop\\test.xlsm"; 
      oXL.Visible = true;

   // Open Staging Workbook
      var oWB = oXL.Workbooks.Add(filename);


   // Place vars from input in correct cell
      oWB.Sheets("Instructions").Cells(1, 5).Value = store;
      oWB.Sheets("Instructions").Cells(2,5).Value = direct;
      oWB.Sheets("SWInventory").Cells(3,2).Value = MdfFloor;
      oWB.Sheets("SWInventory").Cells(3,6).Value = MdfSW;

   //checks to see if 3rd MDF needs to be included
      if (Include == "Yes"){
        oWB.Sheets("SWInventory").Cells(5,2).Value = "Included";
      }

   //fill 2 IDFs in to test atm
      oWB.Sheets("SWInventory").Cells(7,2).Value = "1";
      oWB.Sheets("SWInventory").Cells(7,3).Value = "1";
      oWB.Sheets("SWInventory").Cells(7,4).Value = "SW01";
      oWB.Sheets("SWInventory").Cells(7,6).Value = "EX2200C";
      oWB.Sheets("SWInventory").Cells(8,2).Value = "2";
      oWB.Sheets("SWInventory").Cells(8,3).Value = "2";
      oWB.Sheets("SWInventory").Cells(8,4).Value = "SW02";
      oWB.Sheets("SWInventory").Cells(8,6).Value = "EX2200C";

      window.alert("Filled Sheet Just Fine");
      //run config macro
      oXL.Run("test.xlsm!Module1.makeconfigs");
      window.alert("Process Complete");





}

</SCRIPT>
<Form Name=Input>
<p>
          <label>Store Name</label>
          <input type = "text"
                 name= "StoreName"
                 value = "" />
</p>
<p>
          <label>File Directory</label>
          <input type = "text"
                 name= "FilePath"
                 value = "" />
</p>
<p>
          <label>MDF Floor #</label>
          <input type = "text"
                 name= "MdfFloor"
                 value = "" />
</p>
<p>
          <label>MDF Type</label>
          <input type = "text"
                 name= "MdfType"
                 value = "Enter MDF SW TYpe" />
</p>
<p>
          <label>MDF Include</label>
          <input type = "text"
                 name= "MdfInc"
                 value = "3rd MDF Yes or No?" />
</p>

</form>
<P><INPUT id=button1 type=button value="Start Excel" 
          onclick="AutomateExcel Input.StoreName.Value,Input.FilePath.Value,Input.MdfFloor.Value,Input.MdfType.value,Input.MdfInc.Value">
</P>    

</body>
</html>

更新:

我还没有发现为什么会出现预期的错误,但我确实通过创建一个简单地运行宏的 VBScript 函数来实现解决方法。出于某种原因,VB 可以运行这个特定的宏,但 Jscript 不喜欢。

<!DOCTYPE html>
<html lang="en">
    <body>
        <script language = "VBscript">
         function RunMacro()
            dim oXL
            Set oXL = GetObject(,"Excel.Application")
            oXL.Run "makeconfigs"
         end function
        </script>
        <Script Language = "jscript">
         function AutomateExcel(){
            var oXL = new ActiveXObject("Excel.Application");
            var filename = "D:\\Profiles\\ngwx36\\Desktop\\test.xlsm"; 
            oXL.Visible = true;

            var oWB = oXL.Workbooks.Add(filename);
            RunMacro();

         }
        </Script>

        <P><INPUT id=button1 type=button value="Start Excel" 
          onclick="AutomateExcel()">
</P>    

    </body>
</html>
4

5 回答 5

1

这是错误代码冲突的情况。

JavaScript 引擎将 1004 映射到*语法错误:预期的 ';'*,请参阅JavaScript 语法错误

Office 自动化将代码 1004 映射到运行时错误 1004:应用程序定义的或对象定义的错误

如果您从 JavaScript 调用 Office 组件(在您的情况下为 Excel.Application),则组件中发生的运行时错误 1004 会传播到 JavaScript 引擎,该引擎将此代码映射到语法错误:预期的 ';' .

当然,这种类型的错误传播是彻头彻尾的愚蠢,感谢微软。

因此,您的问题不在于 JavaScript,而在于对 Office 组件的调用,大概是 run() 方法。

于 2013-06-29T13:38:41.663 回答
0

我看到的唯一缺少分号的行是您致电window.alert()

window.alert("Filled Sheet Just Fine")

把它加回来,我猜你会没事的。

于 2013-06-06T16:41:52.230 回答
0

您在这一行的末尾缺少一个分号:

window.alert("Filled Sheet Just Fine")
于 2013-06-06T16:42:02.697 回答
0

Looks like JScript is handling the onclick. Its syntax works with VBScript, but not with JScript. This can be fixed by adding a pseudo protocol:

onclick="VBScript: AutomateExcel Input.StoreName.Value, Input.FilePath.Value, ... "
      JScript expects ; here ---^

You can also use JScript, but then you need to enclose the arguments of AutomateExcel() to parenthesis, though I'm not sure if Input is defined for Jscript.

It's unclear to me, why an online event handler is sometimes interpreted with VBScript and sometimes with JScript. Maybe you want to ask a new question about this.

于 2013-06-07T04:59:22.300 回答
0

//这对我来说很好。

       function call_Macro(){
        try{
        var ExApp;
        var excel_file;
        ExApp = new ActiveXObject("Excel.Application");
        var excel_file = ExApp.Workbooks.Open("D:\\NewFolder\\FSO.xlsm"); 

        ExApp.Run("FSO.xlsm!MacroName");

        excel_file.Close();    // important
        excel_file = null;
        ExApp.Quit();
        ExApp = null;       
        }
        catch(ex)
        {
        alert(ex.message);
        excel_file.Close();    // important
        excel_file = null;
        ExApp.Quit();
        ExApp = null;   
        }
    }
于 2016-11-07T11:37:40.763 回答