Solved

Get reference to Internet Explorer

Posted on 2001-07-01
9
193 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 Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

708 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

19 Experts available now in Live!

Get 1:1 Help Now