Get existing IE via VBA

A compressed combination of the above answers, here’s what works for me.


GetIE Function

(No references required.)

Function GetIE() As Object
'return an object for the open Internet Explorer window, or create new one
  For Each GetIE In CreateObject("Shell.Application").Windows() 'Loop to find
    If (Not GetIE Is Nothing) And GetIE.Name = "Internet Explorer" Then Exit For 'Found!
  Next GetIE
  If GetIE Is Nothing Then Set GetIE=CreateObject("InternetExplorer.Application") 'Create
  GetIE.Visible = True 'Make IE window visible
End Function

Example Usage:

Sub demo()
    Dim ie As Object
    Set ie = GetIE                                        'get new/existing IE object
    ie.Navigate "http://stackoverflow.com", 2             '2=don't keep history
    Do: DoEvents: Loop While ie.Busy or ie.ReadyState <> 4'wait til loaded
    Stop                                                  'do your stuff
    ie.Quit                                               'close IE
    Set ie = Nothing                                      'clean up
End Sub

Leave a Comment