在HTML中运行VBScript - 通过单击按钮调用函数不起作用

时间:2015-02-22 21:59:54

标签: html vbscript

我写了一个VBscript来控制一个excel工作簿,我从命令行调用它没有任何问题。 当我试图从html调用它时出现问题:我一直收到错误说我的“RunBatch未定义”? (现在我正在我的本地测试并使用IE 11.) 首先,我试图将我的vbscript的url添加到(即作为外部文件),因为它不起作用我将我的代码添加到“<script>” 我认为问题在于:

<input type="button" onclick=" RunBatch(fnam)" value="Click To Test" />
但我没有解决它,在线搜索了很多,仍然得到同样的错误

这是我的剧本:

 <html>
    <head>
                <h1>
                <title>Testing Interface</title>
                </h1>
<meta http-equiv="X-UA-Compatible" content="IE=11">
</head>
<script type="text/vbscript" language="VBScript">
optioin explicit
sub RunBatch(fname)
                Dim oWorkBook
                Dim xlObj
                Dim oShell
                Dim oFS
                Dim wsBatch
                Dim wsBatchMap
                Dim filePath
                Set oShell = CreateObject("Wscript.Shell")
                Set xlObj = CreateObject("Excel.Application")
                Set oFS = CreateObject ("Scripting.FileSystemObject")
                Set filePath = oFS.GetFolder("F:\User Files\....\Testing")
                fname = filePath.Path&"\"&fname
                xlObj.DisplayAlerts = False
                xlObj.AskToUpdateLinks = False
                xlObj.AlertBeforeOverwriting = False
                xlObj.Visible = False
 
                Set oWorkBook = xlObj.Workbooks.Open(fname,0)
                Set wsBatch = xlObj.Worksheets("xx")
                Set wsBatchMap = xlObj.Worksheets("xxxxx")
                     xlObj.Workbooks.Open(oShell.ExpandEnvironmentStrings("%APPDATA%")&"\xx.xlam")
              'config ur range
               wsBatchMap.Range("J6") = 1
               wsBatchMap.Range("K6") = 10                
               xlObj.Run "XXX.xlam!xxfunction",   xlObj.Workbooks(oWorkBook.Name), _
                                                                                                                                                                                                                                                                                                                                                 xlObj.Workbooks(oWorkBook.Name).Worksheets("xx"), _
                                                                                                                                                                                                                                                                                                                                                 xlObj.Workbooks(oWorkBook.Name).Worksheets("xxxx"), _
                                                                                                                                                                                                                                                                                                                                                 False                     on error resume next
                if err.number <> 0 Then
                on error goto 0
                oShell.Echo "Runtime Error "&err.number&"workbook will be closed"
                err.clear
                oWorkBook.Save
                oWorkBook.Close                        
                Set oWorkBook = Nothing
                if xlObj.Workbooks.Count = 0 Then
                   xlObj.Quit
                End If
                Set oWorkBook = Nothing
                Set args = Nothing
                Set xlObj = Nothing
                Set oShell = Nothing
                Set oFS  = Nothing          
                document.write ".Done"
End sub
</script>
<body BGCOLOR="white">
                <h1>Testing Interface</h1>
                input Testing File Name: <INPUT TYPE="text" NAME="fname">
                <br />
                <input type="button" onclick=" RunBatch(fnam)" value="Click To Test" />
</body>
</html>
 `

1 个答案:

答案 0 :(得分:2)

  1. 修正拼写错误“optioin explicit”
  2. 修复无效的HTML(至少:</head><body>之间不允许任何内容;将脚本移到头部)
  3. 当按下按钮时,可以看到fnam有一个值(可能是:onclick="RunBatch fname.value"
  4. on error goto 0清除Err对象;信息丢失了。