Using the Internet Explorer Objects
Since the Internet Explorer supports Automation, you can access its functionality from Visual Basic. You can make the objects in the Internet Explorer available to Visual Basic 4.0 through the References dialog box. The References dialog box lists all the objects available to Visual Basic. You access the dialog box by selecting Tools/References from the menu bar. Figure 3-5 shows the References dialog box with a reference set to the Internet Explorer object library. The proper reference is described in the dialog box as "Microsoft Internet Controls."
Once a reference is set in Visual Basic, you can exploit the objects in code. For example, to get an instance of the Internet Explorer browser that can be accessed from Visual Basic, you could use the following code:
Dim MyBrowser As SHDocVw.InternetExplorer Set MyBrowser = New SHDocVw.InternetExplorer
The first line of this code is a variable declaration, and the second line creates an instance of the Internet Explorer. The syntax SHDocVw.InternetExplorer refers to the name of the server, SHDocVw, and the type of object that you want to create, Internet Explorer. SHDocVw itself is actually a server that contains more than just the Internet Explorer objects. This server defines constants and events as well. You can use all of these from Visual Basic to make any Visual Basic application Internet-enabled.
The References dialog box in Visual Basic.
Once you have an instance of the Internet Explorer, you can manipulate it through properties and methods. For example, if you want the browser to appear and navigate to the New Technology Solutions, Inc., web site, you could use the following code:
MyBrowser.Visible = True MyBrowser.Navigate "http://www.vb-bootcamp.com"
You can use this type of coding to add browsing capabilities to Visual Basic projects or to create utilities for the browser. Listing 3-2 shows a complete project in Visual Basic that implements a Favorites list for the Internet Explorer browser. The project uses a small Microsoft Access database to keep track of your favorite URLs. These URLs are placed in a list box, from which users can pick a specified URL and cause the browser to navigate to it. Figure 3-6, on page 51, shows an example of the Favorites list box.
`This demo implements a Favorites list for the Internet `Explorer 3.0. The list of favorite URLs is stored `in a Microsoft Access database. `This demo automatically launches the Internet Explorer. `The user can jump to a favorite URL by selecting it from `the list and clicking on the Navigate button or just `double-clicking on the URL. Option Explicit Public MyBrowser As SHDocVw.InternetExplorer Private Sub Form_Load() `Get an instance of Internet Explorer 3.0 Set MyBrowser = New SHDocVw.InternetExplorer MyBrowser.Visible = True `Fill list box with favorites Call FillList End Sub Private Sub cmdNavigate_Click() If lstFavorites.ListIndex = -1 Then Exit Sub `Use the Navigate method to display the `selected URL MyBrowser.Navigate lstFavorites.List(lstFavorites.ListIndex) End Sub Private Sub lstFavorites_DblClick() cmdNavigate.Value = True End Sub Private Sub FillList() `Author: New Technology Solutions, Inc. `Purpose: Fill list box from Favorites database `6/2/96 Original Dim dbFavorites As DAO.Database Dim rsFavorites As DAO.Recordset `Get favorites from database Set dbFavorites = DBEngine.Workspaces(0) _ .OpenDatabase(App.Path & "\favorite.mdb") Set rsFavorites = dbFavorites.OpenRecordset _ ("SELECT * FROM Favorites", dbOpenSnapshot) If rsFavorites.BOF And rsFavorites.EOF Then Exit Sub lstFavorites.Clear rsFavorites.MoveFirst `Fill list box Do While Not rsFavorites.EOF lstFavorites.AddItem rsFavorites!URL rsFavorites.MoveNext Loop rsFavorites.Close dbFavorites.Close Set rsFavorites = Nothing Set dbFavorites = Nothing End Sub Private Sub cmdExit_Click() Unload Me End Sub
The Favorites list box.