Controlling IE from VB.Net

heabrams

Newcomer
Joined
Aug 2, 2010
Messages
2
I want to give my user the ability to provide the url of a web page. I load ie using the following code:

Dim IE As New SHDocVw.InternetExplorer
IE.Visible = True
IE.Navigate("www.msn.com")

Now, I want my program to wait until ie is closed, and then I want to capture the url at the time of closing. So, how do I wait for ie to complete and is there an ie event such as beforeclosing that will allow me to get the location property?

Please note that I am NOT using the web browser control but rather launching a copy of ie (to make the user's favorites and web history available).

Thanks!
 
Well, here is the answer

1. Dim a variable with form-level scope to hold the ie object when needed along with a form-level variable to hold the url:

Dim IE As SHDocVw.InternetExplorer ' Do not use "new" nere; instantiate only as needed
Dim URL As String

2. Create a handler for the OnQuit event that takes no arguments:

Sub HandleIEQuit()
URL = IE.LocationURL
Dim x As Integer = 1
End Sub

3. Assign a limited-scope handler delegate in the subroutine or procedure creating the browser call:

Private Sub AddURL_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles AddURL.ItemClick
URL = ""
IE = New SHDocVw.InternetExplorer
AddHandler IE.OnQuit, AddressOf HandleIEQuit
IE.Visible = True
IE.Navigate("www.msn.com")
End Sub

4. The delay cab be handled in a variety of ways, but after second thought I decided to eliminate the possibility of creating an unexpected "hang" for my users if they forget to close the browser window.
 
Last edited:
Back
Top