Solved

Get reference to Internet Explorer

Posted on 2001-07-01
9
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 52

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.

752 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