An interesting feature of the WebBrowser control is the fact that its source file, SHDOCVW.DLL, also contains a type library for IE. A type library contains all the information needed to create and control ActiveX objects through Automation (formerly OLE Automation). You can reference SHDOCVW.DLL in your Visual Basic projects to create and control instances of the IE application.
This technique is practical when you are debugging Internet applications written in Visual Basic. By default, IE caches Web pages; however, you will usually want fresh copies of each Web page when debugging. The WebTool add-in sample, shown in Figure 7-7, starts an instance of IE with caching turned off, making it easier to debug Internet applications in Visual Basic.
Figure 7-7. The WebTool add-in always loads fresh Web pages rather than using cached pages.
In addition to the standard connection class (CLS) and registration startup module (BAS) used by all add-ins, the WebTool add-in (WEBTOOL.VBP) includes a single form with a text box for entering URLs and forward and back command buttons.
The following code creates an instance of IE and sets the txtAddress text box to the path in which Visual Basic stores temporary Visual Basic Document (VBD) files while debugging Internet applications. VBD files are discussed in detail in Chapter 9, "Creating Internet Applications."
Option Explicit
'deletes cached page, replaces the Navigate method's
'navNoReadFromCache flag
Private Declare Function DeleteUrlCacheEntry Lib _
"WinInet.DLL" (strURL As String) As Boolean
'Specify the path where VB.EXE is installed
Const VBPath = "file://C:\Program Files\DevStudio\VB\"
`Create an Internet Explorer object variable. Note that
`you must use the "_V1" object to be able to bind to
`the ieView_Quit event
Private WithEvents ieView As SHDocVw.WebBrowser_V1
`In IE3, the following declaration was used:
`Private WithEvents ieView As InternetExplorer
Private Sub Form_Load()
`Establish a reference to application object
Set ieView = GetObject("", "InternetExplorer.Application")
`Be sure Internet Explorer is visible
ieView.Visible = True
`Start with VB.EXE path because that's where VBD
`files are stored during debugging
txtAddress = VBPath
End Sub
Unfortunately, you can't use the GetObject function with the first argument empty to get a running instance of IE. The application doesn't allow it, so you need to start a new instance when you establish your object reference. You also need to be sure to make the instance visible by setting its Visible property to True.
The following code does the navigation work. The DeleteUrlCacheEntry API function guarantees that the Navigate method loads a new version of the URL, rather than loading the URL from an older version cached on your machine.
Private Sub txtAddress_KeyPress(KeyAscii As Integer)
Dim blnResult As Boolean
If KeyAscii = Asc(vbCr) Then
`Eat keystroke
KeyAscii = 0
`Select text
txtAddress.SelLength = Len(txtAddress)
`Delete this URL if it is cached
blnResult = DeleteUrlCacheEntry(ByVal txtAddress)
'Navigate to address
ieView.Navigate txtAddress
End If
End Sub
The ieView object variable is declared using WithEvents, so this form can intercept events from Internet Explorer. The CommandStateChange event is used to enable or disable the forward and back command buttons.
Private Sub ieView_CommandStateChange( _
ByVal Command As Long, _
ByVal Enable As Boolean _
)
`Enable or disable Back and Forward command buttons
`based on whether there is an address to go to
Select Case Command
Case CSC_NAVIGATEBACK
cmdBack.Enabled = Enable
Case CSC_NAVIGATEFORWARD
cmdForward.Enabled = Enable
Case CSC_UPDATECOMMANDS
End Select
End Sub
Private Sub cmdBack_Click()
ieView.GoBack
End Sub
Private Sub cmdForward_Click()
ieView.GoForward
End Sub
Internet Explorer triggers the NavigateComplete event when the Web page has been displayed and triggers the Quit event when the user closes the application. The following code responds to those events in the WebTool add-in.
Private Sub ieView_NavigateComplete(ByVal URL As String)
`Update text box with the final address
txtAddress.Text = URL
txtAddress.SelLength = Len(txtAddress)
`Display the Web page title in the form's caption
Caption = ieView.LocationName
End Sub
Private Sub ieView_Quit(Cancel As Boolean)
`Close this application if user closes Internet Explorer
End
End Sub
All the code used to create the WebTool add-in can be found on the companion CD-ROM.
SEE ALSO
- Chapter 27, "Advanced Programming Techniques," for more information about creating an add-in