Get existing IE via VBA

后端 未结 5 1287
醉梦人生
醉梦人生 2020-12-10 07:48

My goal is to scrape data off multiple webpages on the same site into Excel. I have these pages open in tabs within IE8. I have no other IE windows open.

I have trie

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 08:19

    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
    

提交回复
热议问题