Get existing IE via VBA

后端 未结 5 1135
醉梦人生
醉梦人生 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:06

    I figured it out! Thanks to this thread, the following works:

                   AppActivate " - Microsoft Internet Explorer provided by [my company]"
                   AppActivate " - Microsoft Internet Explorer provided by [my company]"
    

    And it is able to bring forward any site, no matter the naming scheme. Granted, it looks clumsy, but I can't complain if it works.

    Happy day!

    0 讨论(0)
  • 2020-12-10 08:08

    Follow this link to see the code to use an open IE window. http://www.mrexcel.com/forum/excel-questions/553580-visual-basic-applications-macro-already-open-ie-window.html

    0 讨论(0)
  • 2020-12-10 08:11

    Made some changes and it works now

             Function GetIE() As Object
    
                Dim ShellApp As Object, ShellWindows As Object
                Dim IEObject As Object
    
                Set ShellApp = CreateObject("Shell.Application")
    
                Set ShellWindows = ShellApp.Windows()
    
                Dim item As Object
                On Error GoTo 0
                Dim sName As String
    
                   For Each ObjWind In ShellWindows
                    'On Error Resume Next
                     If (Not ObjWind Is Nothing) Then
                        sName = ObjWind.Name
                         If sName = "Internet Explorer" Then
                            Set IEObject = ObjWind
                            Exit For  'No need to continue....
                         End If
                     End If
    
                    Next
    
                If IEObject Is Nothing Then Set IEObject = CreateObject("InternetExplorer.Application")
    
                Set ShellApp = Nothing
    
                Set GetIE = IEObject
    
                End Function
    
    0 讨论(0)
  • 2020-12-10 08:16

    OK, so I notice a potential problem with the first method you attempt using the Shell object.

    You may have an incorrect value for the .FullName property. (when I debug and examin the ShellWindows collection, I see path ""C:\Program Files (x86)\Internet Explorer\"

    Function GetIE() As Object
    
    Dim ShellApp as Object, ShellWindows as Object, i as Long 
    Dim IEObject As Object
    
    Set ShellApp = CreateObject("Shell.Application")
    Set ShellWindows = ShellApp.Windows()
    
    For i = 0 To ShellWindows.Count - 1
        If InStr(ShellWindows.Item(i).FullName, "C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE") <> 0 Then
           Set IEObject = ShellWindows.Item(i)
           Exit For  'No need to continue....
        End If
    Next
    
    If IEObject Is Nothing Then Set IEObject = CreateObject("InternetExplorer.Application")
    
    Set GetIE = IEObject
    
    End Function
    

    You can put this function in your code, just tuck it away somewhere, then whenever you need IE you can call it like:

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题