Solved

Get reference to Internet Explorer

Posted on 2001-07-01
9
210 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

617 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