首页 文章

VBScript - 用户关闭IE用户表单时出错

提问于
浏览
0

我有一个VBScript,它在Internet Explorer窗口中创建一个用户表单 . 此窗口是使用CreateObject(InternetExplorer.Application)创建的 . 问题是,如果用户通过x'ing关闭此窗口,窗口将关闭,但脚本将抛出错误 . 我想避免收到此错误 . 关于错误,我不能更具体,因为它几乎每次都有所不同 . 我的脚本中有一个循环,错误似乎指向我的脚本在用户通过x'ing out关闭程序时执行的循环中的任何行 .

我知道一种方法是使用wscript.CreateObject(InternetExplorer.Application,“IE_”),然后使用:

Sub IE_onQuit
    wscript.quit
End Sub

不幸的是,这对我不起作用,因为我需要运行此代码的设备使用的解释器无法识别wscript对象 . 如果它很重要,我的代码将通过Tecan的EVOware执行,在Windows 11的机器上运行IE 11. EVOware似乎与自己的解释器捆绑在一起,并没有使用Windows操作系统内置的解释器 . 没有关于EVOware的VBscript解释器的文档似乎存在,至少没有我能找到的 . 但是,如果不使用上面描述的IE_onQuit方法,我的代码将在使用Windows解释器直接通过vbs文件执行时抛出相同的错误,因此错误不是EVOware特有的;只是因为EVOware我无法使用任何wscript解决方案 .

我也尝试使用On Error Resume Next,但这也行不通,因为这会导致我的脚本被无限循环捕获并且永远不会关闭,除非我在任务管理器中终止该进程 .

有没有办法实现我的目标(即允许用户x-out窗口而不抛出错误)而不使用wscript?或者,禁用x-out按钮也是一种可接受的解决方案;但是,通过使IE窗口全屏隐藏这些按钮不是 .

这是我的代码示例:

sHTMLCode = some html code

Set IE = CreateObject("InternetExplorer.Application")

With IE

    .ToolBar    = False
    .StatusBar  = False
    .Resizable  = False
    .Height     = 500
    .Width      = 400
    .Left       = 600
    .Top        = 200

    .Visible = True
    CreateObject("WScript.Shell").AppActivate "Internet Explorer"
    .Navigate "about:blank"

    While .ReadyState <> READYSTATE_COMPLETE
        Sleep 0.01
    Wend

    .document.title = "Title"
    .document.body.style.backgroundcolor = "lightgrey"
    .document.body.innerHTML = "<center>" & sHTMLCode & "</center>"

    Do Until Protocol_Type <> "" And Number_of_Plates <> "" And Dye_Source <> ""

        If .document.all.done.value = "clicked" Then
            .document.all.done.value = False
            .
            .
            .
            stuff
            .
            .
            .   
                Protocol_Type = sProtocol_Type
                Evoware.SetStringVariable "Protocol_Type",Protocol_Type

                Number_of_Plates = sNumber_of_Plates
                Evoware.SetDoubleVariable "Number_of_Plates",Number_of_Plates

                Dye_Source = sDye_Source
                Evoware.SetStringVariable "Dye_Source",Dye_Source

            Else MsgBox "Please re-enter your protocol parameters.", vbInformation
            End If
        End If
    Loop
    .document.all.done.value = True
    .Quit
End With

2 回答

  • 1

    这可以吗?

    if Err.Number<>0 then 
      MsgBox "ERROR: " 
      Exit Do 
    End If
    
  • 0

    您可以在脚本的开头尝试:

    On Error Resume Next
    

    然后在Quit语句后捕获错误代码,并忽略导致它的错误代码 . 无论如何,错误是什么?

相关问题