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
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!
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
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
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:
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
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