Solved

Get reference to Internet Explorer

Posted on 2001-07-01
9
197 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 49

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 27

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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 27

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 27

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
This collection of functions covers all the normal rounding methods of just about any numeric value.
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…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

861 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now