title: Quitting the IE in the end of VBA function
body: <p>I implemented several functions which relies on downloading some information from some websites.</p>
  
  <p>The simplest example of such a function is:</p>
  
  <pre><code>Public Function getSomething(webAddress As String)
      Dim html As HTMLObjectElement
      Set html = getWebContents(webAddress)
      Set elems = html.body.getElementsByTagName(tagName)
      ...
  End Function
  </code></pre>
  
  <p>The function for acquire data from websites is:</p>
  
  <pre><code>Public Function getWebContents(webAddress As String) As HTMLObjectElement
      Dim ie As InternetExplorer
      Dim html As HTMLDocument
      Set ie = New InternetExplorer
      ie.Visible = False
      ie.Navigate webAddress
      Do While ie.READYSTATE &lt;&gt; READYSTATE_COMPLETE
          Application.StatusBar = "Trying ..."
          DoEvents
      Loop
  
      Set getWebContents = ie.Document
  
      'close down IE and reset status bar
      'ie.Quit
      Set ie = Nothing
      Application.StatusBar = ""
  End Function
  </code></pre>
  
  <p>The problem is that it seems that I need the line ie.Quit to be uncommented to close the IE instance. But when I uncomment ie.Quit the line</p>
  
  <pre><code>Set elems = html.body.getElementsByTagName(tagName)
  </code></pre>
  
  <p>generates errors.</p>
  
  <p>It seems that I cannot use HTMLObjectElement returned by function getWebContents when IE has been quitted. How to deal with that? I could implement a try...finally block in getSomething function and open ie there and close in the finally block. However I have many functions of a similar nature and making many similar try...finally blocks seems a stupid idea.</p>
  
  <p>Any thoughts?
  Thanks!</p>