How to access current Internet Explorer from VB.Net

E

ewhite2142

I am writing code in VB.Net to access data from the Internet. If I write
the following code, I get access to a new instance of Internet Explorer:

Imports SHDocVw
Dim Br as New InternetExplorer
Br.Navigate(http://www.URL.com)

However, I want to access the instance of Internet Explorer already running,
not create a new instance. I tried the following code, and I get an
exception stating it could not open ActiveX object.

Imports SHDocVw
Dim Br as InternetExplorer
Br=GetObject( , "InternetExplorer.Application")

How do I get access to an existing instance of Internet Explorer?
 
M

Mattias Sjögren

How do I get access to an existing instance of Internet Explorer?

You can use the ShellWindows collection, also part of the ShDocVw
library.



Mattias
 
E

ewhite2142

I see ShellWindows and ShellBrowserWindow in the object browser. The
following code will create a new window with the existing URL, but it does
not use the same window.

Imports SHDocVW
Dim Br as New ShellBrowserWindow
Br.Navigate("URL")

The following code generates "Object reference not set to an instance of an
object" exception (the "New" is not included as above). How do I reference
the existing object?

Imports SHDocVW
Dim Br as ShellBrowserWindow
Br.Navigate("URL")

Thanks,
Ed
 
M

Mattias Sjögren

Ed,
How do I reference the existing object?

Iterate through the ShellWindows collection until you find the window
you're looking for. You could for example do like this

object m = Type.Missing;
foreach ( InternetExplorer ie in new ShellWindows() )
if ( ie.LocationURL == "http://www.foo.com" )
ie.Navigate( "http://www.bar.com", ref m, ref m, ref m, ref m );



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top