Excel VBA 强制关闭 IE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27626253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:38:11  来源:igfitidea点击:

Excel VBA force shutdown IE

excelvbainternet-explorer

提问by Suraj Khosla

I am currently using the following sub to close my IE after automating:

我目前正在使用以下子程序在自动化后关闭我的 IE:

Public Sub CloseIE()
    Dim Shell As Object
    Dim IE As Object

    Set Shell = CreateObject("Shell.Application")

    For Each IE In Shell.Windows
        If TypeName(IE.Document) = "HTMLDocument" Then
            IE.Quit
        End If
    Next
End Sub

This works great but the problem occurs when I try to run the IE code again, I get the following:

这很好用,但是当我再次尝试运行 IE 代码时出现问题,我得到以下信息:

Run-time error '-2147023706 (800704a6)':

运行时错误“-2147023706 (800704a6)”:

Automation error

自动化错误

A system shutdown has already been scheduled.

已安排系统关闭。

After 20 secs, I can re-run the code. Is there any way of "force closing" IE so that I can run the code again directly after without the error?

20 秒后,我可以重新运行代码。有没有办法“强制关闭”IE,以便我可以在没有错误的情况下直接再次运行代码?

EDIT:

编辑:

Heres the code that initiates IE:

下面是启动IE的代码:

Sub testSub()

    Dim IE As Object, Doc As Object, strCode As String

    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True

      IE.Navigate "website name"

        Do While IE.ReadyState <> 4: DoEvents: Loop

        Set Doc = CreateObject("htmlfile")
        Set Doc = IE.Document 

       CODE HERE

       CloseIE

End Sub

采纳答案by David Zemens

Modify your main sub to Quitthe IE application, and then set the object variable to Nothing:

将您的主子修改Quit为 IE 应用程序,然后将对象变量设置为Nothing

Sub testSub()

    Dim IE As Object, Doc As Object, strCode As String

    Set IE = CreateObject("internetexplorer.application")
    IE.Visible = True

      IE.Navigate "website name"

        Do While IE.ReadyState <> 4: DoEvents: Loop

        Set Doc = CreateObject("htmlfile")
        Set Doc = IE.Document 

       '### CODE HERE

       IE.Quit
       Set IE = Nothing

End Sub

You will not need the CloseIEprocedure anymore.

您将不再需要该CloseIE程序。

回答by joe d

I used a simple procedure to create an InternetExplorerobject, navigate, then close it using IE.Quitand Set IE = Nothing.

我使用了一个简单的过程来创建一个InternetExplorer对象,导航,然后使用IE.Quit和关闭它Set IE = Nothing

Apparently after closing, the internet was still running in the background for about another minute (I noticed it using task manager, background processes). I went into Internet Explorer options and unclicked “delete browsing history on exit”.

显然关闭后,互联网仍在后台运行大约一分钟(我使用任务管理器,后台进程注意到它)。我进入 Internet Explorer 选项并取消单击“退出时删除浏览历史记录”。

That fixed my issue. I am not sure why IE takes that long to clear the history and I'm not sure if there is a workaround but it is the only viable option for me thus far.

那解决了我的问题。我不确定为什么 IE 需要那么长时间来清除历史记录,我不确定是否有解决方法,但它是迄今为止我唯一可行的选择。