Excel VBA force shutdown IE

14,943

Solution 1

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

You will not need the CloseIE procedure anymore.

Solution 2

I used a simple procedure to create an InternetExplorer object, navigate, then close it using IE.Quit and 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”.

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.

Share:
14,943
Suraj Khosla
Author by

Suraj Khosla

Updated on June 04, 2022

Comments

  • Suraj Khosla
    Suraj Khosla almost 2 years

    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
    

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

    Run-time error '-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?

    EDIT:

    Heres the code that initiates 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
    
  • Suraj Khosla
    Suraj Khosla over 9 years
    Thanks, this helped as I think it halved the time I got the error but I still got the error. I think I know why and its because I forgot to mention I have two tabs in IE to close, your code only seems to close one. Could you please tell me how to close both tabs?
  • David Zemens
    David Zemens over 9 years
    IE is the application object, the Quit method should close the entire application, is that not what you're experiencing?
  • Suraj Khosla
    Suraj Khosla over 9 years
    It closes the IE ok but I still get the error when I try to run another IE straight after
  • Suraj Khosla
    Suraj Khosla over 9 years
    Yes at: Set IE = CreateObject("internetexplorer.application")
  • David Zemens
    David Zemens over 9 years
    Hmmm. Doesn't sound like it's related to the method of closing the IE then, something else must be causing the error. Have you tried googling for that error message? Alternatively (it's more complicated but could avoid the IE error) you could use an xmlhttprequest instead of IE. Unless you need to use IE...
  • Suraj Khosla
    Suraj Khosla over 9 years
    I found this: support.microsoft.com/kb/189618 I dont really need IE, all I am trying to do is go online and fill out some <input> box's on a browser. Is there any way I can make a simple modification to my code to make this work without the error? The code works fine, its just when its ran multiple times it has a sort of "cooldown time" after each run.
  • David Zemens
    David Zemens over 9 years
    Probably not a simple method, I haven't had time too look at the error specifically although the link you posted is NOT about IE. I think xmlhttprequest would be the way to go but without seeing the rest of your code I don't know if that would be simple or difficult to implement.
  • David Zemens
    David Zemens over 9 years
    This is a similar problem, and I would ask the same questions: "Why not just navigate the page again or refresh. i.e. loop to the navigate not the create. Why close IE if you are going to use it again?"
  • David Zemens
    David Zemens over 9 years
    Another similar problem suggests that maybe you need to reboot? If you reboot, does the problem persist?