VBS代码不能在HTA中使用(WScript声明)

我目前正在开发一个界面,让用户select哪个报告,他们想要生成,但我有问题,我的“WScript”的声明。

我也附上了错误。 正如我已经固定行间的间距不注意行号 – 我粗线的,我错了。 真的很感谢任何帮助(尽我所能,正确格式化我的问题)。

<html> <title>Report Generation</title> <head> <HTA:APPLICATION APPLICATIONNAME="Master Report Generation" SCROLL="yes" SINGLEINSTANCE="yes" WINDOWSTATE="normal"> </head> <style> BODY { background-color: buttonface; Font: arial,sans-serif margin-top: 10px; margin-left: 20px; margin-right: 20px; margin-bottom: 5px; } .button { width: 91px; height: 25px; font-family: arial,sans-serif; font-size: 8pt; } td { font-family: arial,sans-serif; font-size: 10pt; } #scroll { height:100%; overflow:auto; } SELECT.FixedWidth { width: 17em; /* maybe use px for pixels or pt for points here */ } </style> <script language="vbscript"> Option Explicit Dim WinWidth : WinWidth = 350 Dim WinHeight : WinHeight = 250 Window.ResizeTo WinWidth, WinHeight Sub CheckBoxChange If CheckBox(0).Checked Then ExecuteScoreCard() Else MsgBox "CheckBox is not checked" End If End Sub Sub ExecuteScoreCard() Dim sitecode Dim objExcel Dim objApp Dim objWorkbook Dim objSheet Dim scriptdir Dim oFSO Set oFSO = CreateObject("Scripting.FileSystemObject") <b>scriptdir = oFSO.GetParentFolderName(WScript.ScriptFullName)</b> Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(scriptdir & "\SCORECARD.xlsm") Set objSheet = objWorkbook.Worksheets("Cover Tab") objSheet.Cells(4, 2) = sitecode objExcel.Run "RefreshConns" WScript.Sleep 75000 objExcel.ActiveWorkbook.SaveAs scriptdir & "\Scorecards\" & "SCORECARD_" & sitecode & "_" & Year(Now()) & Month(Now()) & Day(Now()) & "_" & Hour(Now()) & Minute(Now()) &".xlsm", 52 objExcel.ActiveWorkbook.Close objExcel.Quit MsgBox("Successfully generated scorecard.") End Sub </script> <body> Site Code: <input type="inputbox" name="sitecode"> <br> <input type="checkbox" name="CheckBox"> Scorecard <br> <input type="checkbox" name="CheckBox"> Report2 <br> <input type="checkbox" name="CheckBox"> Report3 <br> <br> <input type="submit" name="accept" value="Submit" onclick="CheckBoxChange"> </body> </html> 

在这里输入图像说明

使用Windows脚本宿主(cscript.exe或wscript.exe)运行脚本时,内置的WScript对象就在那里。 另一方面,一个HTA文件基本上是运行IE浏览器(虽然在一个允许本地文件系统和COM对象访问的模式)。 没有内置的WScript对象,尽pipe可以从WSH中使用的COM对象也可以在HTA中使用(例如,参见本文中我很快发现的使用WScript.shell对象 )。

你需要在HTA中以更像networking的方式来做事情。 使用setTimeout或类似的延迟function,直到以后。 我做了一个快速的search,如何从HTA中获取当前path并find一个页面 ,尽pipe可能还有其他的方法。

另请参阅这个相关的问题 。

这里是一个示例HTA文件,显示了WScript.ScriptFullNameWScript.Sleep方法的解决方法:

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>WScript workaround</title> <HTA:APPLICATION ID="objHTA" > </head> <body> <script Language="VBScript"> ' output full path to HTA document.write GetFullName() ' wait 5 seconds and close Sleep 5 self.close Function GetFullName() GetFullName = Replace(objHTA.commandLine,"""","") End Function Sub Sleep(lngDelay) CreateObject("WScript.Shell").Run "Timeout /T " & lngDelay & " /nobreak", 0, True End Sub </script> </body> </html>