Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identifying one of multiple Explorer/IExplorer windows

Status
Not open for further replies.

CautionMP

Programmer
Dec 11, 2001
1,516
US
Scenario:
* I have data contained in a web page that uses an https connection that I need to capture.
* During regular use I have at least 2 Internet Explorer windows open, and 1 possibly 2 Explorer windows open.

Question:
What is the trick to differentiating the difference between an Internet Explorer window and a regular Explorer window?

Background:
I have the routines written to find a single instance of IExplorer.exe, and if it contains HTML, return the HTML and normalize it to a dataset and dump into Access/Excel. It works fine if, and only if, I have one instance of (or the first instance of) IExplorer.exe running and that instance of IExplorer.exe is viewing a web page. Otherwise I get unexpected results - usually an error.

Any insight the community can provide is greatly appreciated.
 
I tried this code on my box and it always returned the first instance of Internet Explorer:
Code:
Sub GetIE()
  Dim ie As Object
  
  On Error Resume Next
  Set ie = GetObject(, "InternetExplorer.Application")
  If Err <> 0 Then
    Err.Clear
    Debug.Print "Couldn't find it"
  Else
    Debug.Print ie.LocationURL
    Debug.Print ie.LocationName
    Debug.Print ie.Document.documentElement.innerHTML
  End If
  
End Sub
Since I delcared it as type Object, I had to set a break point in the code after I captured ie, then I opened the "Locals Window" to browse all its properties.

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Although, if I open Explorer first, it grabs that instead...more research needed [worm]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I've been asked this question before and I never got past a certain point in solving it. I know how to enumerate the windows until I find the one I'm looking for, but I don't know how to attach to it as a COM object. I looked into some C documentation the last time and ran into a dead end, I was hoping someone else had more to say about it.

At any rate, here's how you can loop through the running instances of Internet Explorer (this ignores regular Explorer instances):
Code:
[green]'~~~~~~~~~~~  API STUFF ~~~~~~~~~~~~~[/green]

Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long) As Long

Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2

[green]'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[/green]

Public Sub FindIEWindows()
On Error GoTo ErrHandler

  Dim strWindowText As String
  Dim strClassname As String
  Dim lngLength As Long
  Dim Hwnd As Long

  Hwnd = GetWindow(Access.hWndAccessApp, GW_HWNDFIRST)
  
  Do While Hwnd <> 0
    strWindowText = Space(256)
    lngLength = GetWindowText(Hwnd, strWindowText, 255)
    strWindowText = Left(strWindowText, lngLength)
    strClassname = Space(256)
    lngLength = GetClassName(Hwnd, strClassname, 255)
    strClassname = Left(strClassname, lngLength)

    If strClassname = "[blue]IEFrame[/blue]" Then
      Debug.Print "Found IE!"
      Debug.Print "     o---> Title Text: " & strWindowText
    End If
    Hwnd = GetWindow(Hwnd, GW_HWNDNEXT)
  Loop
  
ExitHere:
  Exit Sub
ErrHandler:
  Debug.Print Err, Err.Description
  Resume ExitHere
End Sub
I opened 4 IE windows on my box and ran the procedure and the output was this (truncated to fit):
Code:
Found IE!
     o---> Title Text: Microsoft: Access Modules...
Found IE!
     o---> Title Text: GetObject Function - Microsoft...
Found IE!
     o---> Title Text: Google Search: GetObject...
Found IE!
     o---> Title Text: Google Search: ClassName...
All we have to do is figure out how to attach to the IE instance once it's found...[reading]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
OK, I think I found something that will work - at least it works on my box...you'll have to try it on yours and let me know what happens. The function loops through the windows in the "Shell.Application" object and locates the InternetExplorer instances. It takes a URL as an argument, and locates the correct instance of IE by its currently loaded "LocationUrl" property. Here's the function:
Code:
[green]'********************
'*  §lamKeys §oftware 2000® (VBSlammer)
'*
'*  @CREATED  :   1/21/2005 1:09:27 AM
'*  @PARAMS   :   strUrl - the http:// address of the file.
'*            :   objIE  - IE object.
'*  @RETURNS  :   True if found, False otherwise.
'*  @NOTES    :
'*  @MODIFIED :
'********************[/green]
Function GetIExploreInstance(ByVal strUrl As String, ByRef objIE As InternetExplorer) As Boolean
On Error GoTo ErrHandler

  Dim shl As Object
  Dim ie As InternetExplorer

  Set shl = CreateObject("Shell.Application")
  
  For Each ie In shl.Windows
    If ie.LocationURL = strUrl Then
      Set objIE = ie
      GetIExploreInstance = True
      Exit For
    End If
  Next
ExitHere:
  Exit Function
ErrHandler:
  Debug.Print Err, Err.Description
  Resume ExitHere
End Function

Here's the testing routine:
Code:
Sub TestGetIE()
  Dim ie As InternetExplorer
  
  If GetIExploreInstance("[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=985288&page=6",[/URL] ie) Then
    Debug.Print "Page Title: " & ie.Document.Title
    Debug.Print "Page Body: " & ie.Document.Body.outerHTML
  Else
    Debug.Print "Instance Not Found!"
  End If
End Sub

Sometimes you have to think outside the box...[idea]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I got the first snpit to work, I've been working on the UI to display all the open IE windows to the user, allowing them to select which window has the data thats needed, then pass a reference to the object back to my code that extracts the data. (Unfourtunately I'm working in Excel for this project so development is running a little slow).

I will take a look at the second snipit. Using the URL is tricky for my environment because I'm dealing with https applications (long URL's) and web applications that are hsoted from server farms so 81% of the URL is the same for all applications.

B.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top