Search
Enter Keywords:
Home
Web Browsing and Form Completion with Visual Basic 6 - Part 1 PDF Print E-mail
User Rating: / 4
PoorBest 
Written by Michael Salsbury   
Friday, 28 October 2005
Although the capability exists in Visual Basic 6.0 (VB6) to do some very nifty things with web sites, it's something I haven't seen documented anywhere in a form that a beginning or intermediate VB6 Programmer could make any sense of.  It took me a while to sort it out myself, which may be a part of the issue.  I'm hoping within this series of articles to help others understand how to use the Visual Basic 6 Webbrowser object effectively, or at least get them started.  I'm by no means an expert at this but I've been able to do most things I've wanted to be able to do involving making a VB6 program interact with a web site.
When you embed a Webbrowser control in a VB6 form, you're actually making the power of Internet Explorer available to your application.  Since the Webbrowser control is subservient to your VB6 program, you can use the control to effectively "automate" web browsing tasks by telling your program to manipulate the contents of the web browser.  For instance, by telling the Webbrowser control to change the value of a text field in the browser window, you are essentially telling Internet Explorer to fill in that text field for you.  It's going to take us a little while to get the point where we're doing that here, but we're going to get there.

The Basics

The WebBrowser control provides a number of methods, events, and properties that will allow us to have nearly complete control over what happens within it. 

The more useful methods for our purposes in this series of articles are:

Navigate:  Allows you to tell the WebBrowser control to open a specific URL, just as if you were to manually type the address into Internet Explorer's Address field and hit Enter.

Example:  Webbrowser1.Navigate "http://www.mikesalsbury.com"

This tells the Webbrowser1 object to navigate to the URL specified in quotes.

Refresh:  Allows you to refresh the document in the WebBrowser control's display, just as if you hit the Refresh button in an Internet Explorer window.

Example:  Webbrowser1.Refresh

Tells the Webbrowser1 object to refresh the document currently being displayed.

Stop:  Allows you to stop what the WebBrowser control is doing, just as if you hit the Stop button in an Internet Explorer window.

Example:  Webbrowser1.Stop

This tells the Webbrowser1 object to stop loading the documet.

The WebBrowser object also provides a number of events which can be used to trigger your program to take action, including the following:

BeforeNavigate2:  Allows the program to change the browser's behavior before the WebBrowser control attempts to navigate to a new document.

DocumentComplete:  Allows the program to take action when a document in the WebBrowser control has completely loaded.

The WebBrowser object also supplies information via a number of properties, including:

Document:  The document currently being displayed in the WebBrowser control's window.  This property is an object which has a number of properties of its own, which will be covered later.  By examining and changing objects with the Document object, your program can interact with and manipulate what's seen in the WebBrowser window.

LocationURL:  The URL of the document currently being viewed in the WebBrowser control's window.

Let's dive in and look at a some of these in an actual example.

Example 1: View the HTML Source of a Web Page

This isn't the most practical example, since you could accomplish this same thing by simply loading a page in Internet Explorer, right-clicking it, and choosing "View Source" from the context menu.  But it does give a simple example of how to interact with the WebBrowser control, so it's useful from that perspective.  What we're going to do is develop a program that loads a web page in the WebBrowser control and displays it source code in a large TextBox at the bottom of the window.

In the Form creator, create a Visual Basic form that resembles the following:

Creating the Form
Creating the Form

To add the WebBrowser control to a project, you'll need to add the "Microsoft Internet Controls" references and components to your project.  To add the references, go to the Projects menu and choose "References", then select the Microsoft Internet Controls as illustrated below:

Adding the Reference
Adding the Reference

Then add the component by going to Projects, Components, and selecting "Microsoft Internet Controls" as illustrated below:

Adding the Component
Adding the Component

To add the WebBrowser control to the form, click the WebBrowser icon in the control picker on the left side of the VB6 window.  It looks like a little globe.  The cursor should change to a crosshair.  Draw a box on the form using the crosshair that fills the area you want the WebBrowser control to occupy.

The small text box at the top of the form should be named "txtURL".  The button labeled "OpenURL" should be named "btnOpenURL".  The Web Browser object should be named "WebBrowser1".  The large text box at the bottom of the form should be named "txtHTMLDisplay".

Double-click the "Open URL" button to bring up its code.  Enter the following code:

Private Sub btnOpenURL_Click()
   WebBrowser1.Navigate txtURL.Text
End Sub

This code merely reads the text inside the txtURL text field and tells the WebBrowser1 object to navigate to that location.  It doesn't make any attempt to validate the URL.

Add the following code to the Webbrowser1_DocumentComplete event:

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As _
     Object, URL As Variant)

   txtHTMLDisplay.Text = WebBrowser1.Document.Body.innerhtml
   txtURL.Text = WebBrowser1.LocationURL
End Sub

This code is executed whenever a new document is loaded in the WebBrowser1 window.  It retrieves the HTML of the WebBrowser1 Document's "Body" object, specifically retrieving the "innerhtml".  This is the HTML of the document between the "<BODY>" and "</BODY>" tags.  For documents using frames, this will usually be just the parent frame.

Run the example.  A window similar to the following should appear:

After Launching the Application
After Launching the Application


Click the "Open URL" button to load the URL in the text box at the top of the screen.  Wait while it loads the page, watching the text box at the bottom of the window.  Once the page has loaded, you should see a display similar to the following:

An Opened Web Page
An Opened Web Page and its HTML


In the above example, we see my home page loaded in the WebBrowser1 control and the HTML text of my home page in the text box at the bottom of the window.

Notice that if you click any links in the WebBrowser1 control display that the control navigates to the link you clicked on and updates the HTML in the text box at the bottom of the window.  That's because the "DocumentComplete" window is executed any time any document is loaded in the WebBrowser1 window, whether it was loaded by the program or clicked on by the user, etc.

With some additional coding of your own, you could add some interesting functionality to this simple application.  For example, you could add a button that copies the contents of the HTML display box into a text file, giving you a local copy of the HTML part of the web page you're currently viewing.  Using some of the other methods listed earlier, you could turn this into a fully-functional web browser of your own, though I'm not sure why you'd want to do that since Internet Explorer is already available on your system.

What we've learned to do here is how to tell the WebBrowser control to open a particular page and how to get the property of an object in the document currently displayed in the WebBrowser control.  Much of what we're going to be doing from this point on is as simple as this, but getting to the precise object we're looking for is going to be the tricky bit.  I'll share with you my tips to finding that object and manipulating it in the next installment.

Related Blogs:

Related Links:

Last Updated ( Friday, 24 March 2006 )
< Previous   Next >

Main Menu
Home
Blog
Photos
Links
Search
Site Index
Feedback
Administrator
Featured Links
BlogInspiration
SpamToons
Shawn Prince's Blog
Jack Ludwig's Blog
Mike Cramer's Site
Fark
Slashdot
Woot!
Cigar Envy
John Kricfalusi's Blog
CigarBlog 101
Cigars 101 Forum
Sponsored Links


View Site Stats