Excel VBA强制关机IE [英] Excel VBA force shutdown IE

查看:1599
本文介绍了Excel VBA强制关机IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下子句关闭我的IE自动化:

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

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

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

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

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

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

自动化错误

系统关机有已经安排了。

A system shutdown has already been scheduled.

20秒后,我可以重新运行代码。有没有任何方法强制关闭IE,以便我可以在没有错误后直接运行代码?

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?

编辑:

发起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


推荐答案

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

Modify your main sub to Quit the IE application, and then set the object variable to 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

您将不再需要 CloseIE 程序。

这篇关于Excel VBA强制关机IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆