运行 HTML 中的 VBScript - 通过单击按钮调用函数不起作用
Run a VBScript in HTML - invoke function by clicking a button doesn't work
我写了一个 VBscript 来控制 excel 工作簿,从命令行调用它没有任何问题。
当我尝试从 html 调用它时出现问题:我一直收到错误消息说我的 "RunBatch is undefined "? (现在我正在本地测试并使用 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>
`
- 更正拼写错误"optioin explicit"
- 修复无效的HTML(至少:
</head><body>
之间不允许有任何内容;将脚本移到头部)
- 看到fnam有一个值,当按钮被按下时(可能是:
onclick="RunBatch fname.value"
)
on error goto 0
清除Err对象;信息丢失。
我写了一个 VBscript 来控制 excel 工作簿,从命令行调用它没有任何问题。
当我尝试从 html 调用它时出现问题:我一直收到错误消息说我的 "RunBatch is undefined "? (现在我正在本地测试并使用 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>
`
- 更正拼写错误"optioin explicit"
- 修复无效的HTML(至少:
</head><body>
之间不允许有任何内容;将脚本移到头部) - 看到fnam有一个值,当按钮被按下时(可能是:
onclick="RunBatch fname.value"
) on error goto 0
清除Err对象;信息丢失。