Search Tools Links Login

Dynamically Add WebBrowser Control at runtime without a Reference (V. 2)

Allows VB applications to determine at run-time if Internet Explorer (4.0 or later) is installed, and if so, creates a WebBrowser. If not, a trappable error allows program to continue. This is an enhanced version of a previous article, showing how to capture the events of the created object.

Original Author: Dave Slinn


On a VB6 Form, add a Button control in the upper
left corner and a Listbox control underneath the button and down the left hand
side of the form.  Events raised by the WebBrowser will be displayed in the

Place the following code into a standard VB 6.0 form.

Private WithEvents
m_WebControl As VBControlExtender

Private Sub Form_Resize()

On Error Resume Next

   Me.List1.Height = Me.ScaleHeight - Me.List1.Top

   ' resize webbrowser to fill form next to listbox

   If Not m_WebControl
Is Nothing Then

     m_WebControl.Move Me.List1.Left + Me.List1.Width + 30, 0, ScaleWidth - (Me.List1.Left + Me.List1.Width + 30), ScaleHeight

   End If

End Sub

Private Sub Command1_Click()

On Error GoTo ErrHandler

   ' attempting to add WebBrowser here ('Shell.Explorer.2' is registered

   ' with Windows if a recent (>= 4.0) version of Internet Explorer is installed

   Set m_WebControl = Controls.Add("Shell.Explorer.2", "webctl", Me)

   ' if we got to here, there was no problem creating the WebBrowser

   ' so we should size it properly and ensure it's visible

   m_WebControl.Move Me.List1.Left + Me.List1.Width + 30, 0, ScaleWidth - (Me.List1.Left + Me.List1.Width + 30), ScaleHeight

   m_WebControl.Visible = True

   ' use the Navigate method of the WebBrowser control to open a

   ' web page

   m_WebControl.object.navigate ""

Exit Sub


   MsgBox "Could not create WebBrowser control", vbInformation

End Sub

Private Sub m_WebControl_ObjectEvent(Info
As EventInfo)

On Error GoTo ErrHandler

   Dim i As Integer

   Dim evp As EventParameter

   ' display the event that was raised in the listbox

   Me.List1.AddItem "Event Raised: " & Info.Name

   For Each evp In Info.EventParameters

      Me.List1.AddItem " " & evp.Name & " (" & evp.Value & ")"

   Next evp

   Me.List1.ListIndex = Me.List1.NewIndex

Exit Sub


   If Err.Number = -2147024809

      Me.List1.AddItem " " & evp.Name & " (#ERROR)"

      Resume Next

   End If

End Sub

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 103 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.