Solved

Get reference to Internet Explorer

Posted on 2001-07-01
9
200 Views
Last Modified: 2007-12-19
How can I get a reference to an existing instance of Internet Explorer?  I'm trying to do something like this:

Dim ieBrowserRef as InternetExplorer
Dim i as Integer
Dim colBrowsers as Collection

for i = 1 to [total number of running browsers]
   set ieBrowserRef = [an existing browser instance]
   colBrowsers.add ieBrowserRef
next
0
Comment
Question by:dfc106
9 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6243440
Hi, try to use the FindWindow API to get the handle of running window ?

Here is a sample that Close an application using the APIs:

Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub cmdClose_Click()
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, Text1.Text)
    Debug.Print winHwnd
    If winHwnd <> 0 Then
        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
        If RetVal = 0 Then
            MsgBox "Error posting message."
        End If
    Else
        MsgBox Text1.Text + " is not open."
    End If
End Sub

'Hope this will give you some idea. Comment if i'm wrong, thanks.
0
 

Author Comment

by:dfc106
ID: 6243457
I can get the handles by using the enumerate window api, but from there I don't know how to get an InternetExplorer reference.  Basically I want to be able to recieve events from an existing IE.  I can get the events if my program starts IE, but I don't know how to get them if IE was started some other way.  Thanks for helping.
0
 
LVL 28

Accepted Solution

by:
Ark earned 75 total points
ID: 6243512
Hi
Here is the basic:

   Dim SWs As SHDocVw.ShellWindows
   Set SWs = New SHDocVw.ShellWindows
   Dim var As SHDocVw.InternetExplorer
   Set mCol = Nothing
   Set mCol = New Collection
   For Each var In SWs
       mCol.Add var
   Next
   Set SWs = Nothing
   Set var = Nothing

For full working sample take a look on my code at
http://www.freevbcode.com/ShowCode.Asp?ID=1606

Cheers
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 22

Expert Comment

by:CJ_S
ID: 6243922
To receive events from Internet Explorer you may want to dive in Dynamic Data Exchange. I have recently created an application which could be used to rewceive events from Netscape. IE has the same DDE topics available...

if you want a sample, ask and I can email it to you...

Regards,
CJ
0
 
LVL 2

Expert Comment

by:WalterM
ID: 6244295
If your component runs from within Internet Explorer, you can use the shell32.dll API call SHGetInstanceExplorer:

--- code starts here ---

Private Declare Sub SHGetInstanceExplorer(ppUnk As Object)

Dim ieBrowserRef as InternetExplorer
Dim i as Integer
Dim colBrowsers as Collection

for i = 1 to [total number of running browsers]
  SHGetInstanceExplorer ieBrowserRef
  colBrowsers.add ieBrowserRef
next

--- code ends here ---

Michel
0
 

Author Comment

by:dfc106
ID: 6245080
Ark,

That looks like what I want but it doesn't seem to always get every running instance.  

I put the following code on a new exe project.  I start a new browser.  Then start another browser by opening a link in the first browser in a new window.  The code should close both instances but it only closes the first instance.  Am I missing something?  Is there a way to get a reference to the second instance?  Thanks

Private Sub Form_Load()
   Dim x As SHDocVw.ShellWindows
   Dim y As SHDocVw.InternetExplorer

   Set x = New SHDocVw.ShellWindows
   Debug.Print x.Count
   For Each y In x
      Debug.Print y.Name
      y.Quit
   Next
End Sub

0
 
LVL 28

Expert Comment

by:Ark
ID: 6246667
Hi
Take a look at my full code from link above. There are WindowRegistered/WindowRevoked events and this is a good place to update Explorer's collection.

Cheers
0
 

Author Comment

by:dfc106
ID: 6246950
Thanks Ark.  The code example is excellent, I should be able to figure my problem out from that.  But did you know the program blows up anytime a window spawns a new window either by opening a link in a new window, or javascript opening a new window?
0
 
LVL 28

Expert Comment

by:Ark
ID: 6247335
Hi
Not sure, but check my another sample:

http://www.freevbcode.com/ShowCode.Asp?ID=1308

Cheers
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question