VBScript打开了两个IE窗口,但应该只有一个

时间:2013-04-03 15:33:08

标签: internet-explorer object vbscript

我有一个VBScript,ping一些服务器并输出结果(这个Pastebin中的完整代码)。该脚本的一部分显示了一个IE窗口,其中包含“请等待”消息,但由于某种原因,打开了两个IE窗口(并按照下面的代码填充),但只有一个窗口在我Quit时关闭在剧本的最后。

这是导致我麻烦的功能。似乎第一行(objExplorer.Navigate)正在按要求打开一个窗口,但在此之前Set objExplorer = CreateObject("InternetExplorer.Application")也打开了一个窗口。

有谁知道如何阻止这种情况发生?感谢。

' Display the progress box
Set objExplorer = CreateObject("InternetExplorer.Application")

display_progress(objExplorer)
main() ' Do stuff, see Pastebin for full code
close_progress(objExplorer)
output_results() ' Show user the results, see Pastebin for full code

Private Function display_progress(objExplorer)

    objExplorer.Navigate "about:blank"
    objExplorer.ToolBar = 0
    objExplorer.StatusBar = 0
    objExplorer.Left = 600
    objExplorer.Top = 374
    objExplorer.Width = 400
    objExplorer.Height = 152
    objExplorer.Visible = 1

    Dim strText, strButton

    strText = "<div id=""text""><p>Please wait, servers are being pinged.</p><p>Results will be displayed as soon as they are ready.</p></div>"
    strButton = "<div id=""buttons""><input type=""button"" name=""submit"" value=""Cancel"" onclick=""window.open('', '_self', ''); window.close();"" /><div class=""clear""></div></div>"

    objExplorer.Document.Body.Style.Font = "11pt 'Halvetica'"
    objExplorer.Document.Body.Style.Cursor = "wait"
    objExplorer.Document.Title = "Server ping script"
    objExplorer.Document.Body.InnerHTML = strStyle & strText & strButton    

End Function

Private Function close_progress(objExplorer)

    objExplorer.Document.Body.Style.Cursor = "default"
    objExplorer.Quit

End Function

2 个答案:

答案 0 :(得分:0)

大卫,你能否在代码中的'wscript.quit'(第14行)之前加入以下语句并查看?

Set objexplorer = nothing

另外我在你的函数'force_cscript'中看到你使用了一个objshell对象然后使用'wscript.quit'。如果创建对象,请在退出脚本之前将其设置为空或空值。因此在函数'force_cscript'中,在'wscript.quit'之前包含以下语句。

Set objshell = nothing

如果不这样做,对象将继续在后台运行。每次运行脚本时,您的计算机都会变得慢一些,慢的bcos您的对象在后台仍然有效。

答案 1 :(得分:0)

为什么force_cScript内的For..Next函数被Main调用到CScript.exe sub? 如果脚本没有以Call force_cScript() Set objExplorer = CreateObject("InternetExplorer.Application") '... 开头,那么这个函数只是重启脚本 足以在开头只调用一次,并且创建IE对象之前

{{1}}