Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
Displaying HTML Documents
Using Internet Explorer
The "Display All Courses" Example App
 Topics
  From The Lesson
About Visual Basic on
IE in VB 6

 
About Visual Basic on
Opening multiple IE objects in VB 6

 
About Visual Basic
The ExtraGrid Demo

 
Microsoft MSDN
The AxImp Utility

 
Microsoft MSDN
IE Object Reference

 
Microsoft MSDN
Using IE Events in VB.NET

 

To illustrate this technique, let's build a simple application that displays the "raw HTML" for each of the articles in this tutorial directly from my hard disk. You can see the end result at the top of the first page of this lesson. (None of the graphics are displayed because they're added to the page by the About.Com web servers, but it's possible to display the text.) I might want to have something like this to page through the individual articles quickly. (I don't, but I might! But you could use the same technique to display, for example, recipies, favorite pictures, old love letters, whatever, using a web browser.)

I can do this because the articles are all named using a standard name:

blecvbsbsXXYY.htm

where XX is the lesson number and YY is the page number. This is complicated slightly because the lessons are also stored on my hard drive in individual folders that also have a standard name:

VBSBSXX

where XX is again the lesson number.

The basic tasks this application will perform, then, are ...

  • Display the first page (a default start page for lesson 01, page 01).
  • Add 1 to the page number until the Microsoft Web Browser component detects an error, then ...
  • Add 1 to the lesson number and restart the page number at 1.
It isn't quite as simple as that, however. To illustrate what can go wrong, here's a simplification of my first try at writing the program. After adding the Microsoft Web Browser and a button, named DisplayNextFile, I wrote some code like this:
Dim CourseCount As Int16 = 1
Dim PageCount As Int16 = 1

Private Sub Form1_Load {deleted}
    AxWebBrowser1.Navigate({create path string})
End Sub

Private Sub DisplayNextFile_Click {deleted}
    PageCount += 1
    AxWebBrowser1.Navigate({create path string})
End Sub

Private Sub AxWebBrowser1_NavigateError {deleted}
    CourseCount += 1
    PageCount = 1
    AxWebBrowser1.Navigate({create path string})
End Sub

The "create path string" code, by the way, looked like this:

"{constant basic path}\VBSBS" _
	& Microsoft.VisualBasic.Right("0" _
	& CStr(CourseCount), 2) _
	& "\blecvbsbs" _
	& Microsoft.VisualBasic.Right("0" _
	& CStr(CourseCount), 2) _
	& Microsoft.VisualBasic.Right("0" _
	& CStr(PageCount), 2) _
	& ".htm"

This didn't work because the Navigate method inside the NavigateError event would never bring up the first page in the "next" folder. (For example, if blecvbsbs0910.htm, page 10 of lesson 09, didn't exist and triggered the NavigateError event, the new URL, blecvbsbs1001.htm, would never be displayed. It turns out that the execution of event subroutine is suspended after the "404" page not found error is displayed.

You can certainly program around this problem, but the code for doing it isn't simple. Check out this Microsoft MSDN page if you want to dig into it. But there are other ways to accomplish the same goal. This code, which uses the web browser DocumentComplete event, is much less complex and it does work.

Dim CourseCount As Int16 = 2
Dim PageCount As Int16 = 1
Dim urlStr As String
Dim ErrPageNotFound As Boolean = False

Private Sub Form1_Load {deleted}
    AxWebBrowser1.Navigate({create path string})
End Sub

Private Sub DisplayNextLesson_Click {deleted}
    PageCount += 1
    AxWebBrowser1.Navigate({create path string})
End Sub

Private Sub AxWebBrowser1_NavigateError {deleted}
    CourseCount += 1
    PageCount = 1
    urlStr = "{create path string}" 
    ErrPageNotFound = True
End Sub

Private Sub AxWebBrowser1_DocumentComplete {deleted}
    If ErrPageNotFound = True Then
        AxWebBrowser1.Navigate(urlStr)
        ErrPageNotFound = False
    End If
End Sub

The moral of this story is, "Where there is a will, there's a way!"

Return to first page >>>   The Microsoft Web Browser Component
Page 1, 2, 3, 4

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.