Using the WebBrowser Control

The Microsoft WebBrowser control is an ActiveX control that you can use on your application’s forms to browse Web sites, view Web pages and other documents, and download data located on the Internet. The WebBrowser control is useful in situations where you don’t want to disrupt the work flow in your application by switching from Microsoft Access to a Web browser or other document-viewing application.

The WebBrowser control can display any Web page that Microsoft Internet Explorer version 3.0 can display. For example, the WebBrowser control can display pages that include any of the following features:

In addition to opening Web pages, the WebBrowser control can open any ActiveX document, which includes most Microsoft Office documents. For example, if Microsoft Office is installed on a user’s computer, an application that uses the WebBrowser control can open and edit Microsoft Excel spreadsheets, Microsoft Word documents, and Microsoft PowerPoint presentations from within the control. Similarly, if Microsoft Excel Viewer, Microsoft Word Viewer, or Microsoft PowerPoint Viewer is installed, users can view those documents within the WebBrowser control.

With the WebBrowser control, users of your application can browse sites on the World Wide Web, as well as folders on a local hard disk and on a local area network. Users can follow hyperlinks by clicking them or by typing a URL into a text box. Also, the WebBrowser control maintains a history list that users can browse through to view previously browsed sites, folders, and documents.

Note   Additional ActiveX controls that you can use to work with content on the Internet or an intranet are available in Microsoft Office 97, Developer Edition.

Adding the WebBrowser Control to a Form

Before you can add the WebBrowser control to a form, you must have Microsoft Internet Explorer version 3.0 or later installed.

If you purchased Microsoft Office 97 on CD-ROM, you can install Microsoft Internet Explorer version 3.01 by running Msie301.exe from the Iexplore subfolder in the ValuPack folder.

If you purchased Microsoft Access 97 on floppy disks, or if you prefer to install from the Web, you can download and install the latest version of Microsoft Internet Explorer from http://www.microsoft.com/ie/download/.

Once you have Microsoft Internet Explorer version 3.01 installed, the WebBrowser control is automatically registered and is available in form Design view.

Û To add the WebBrowser control to a form

  1. Open the form in Design view.

  2. In the toolbox, click the More Controls tool.

    A menu appears that lists all the registered ActiveX controls in your system.

  3. On the menu of ActiveX controls, click Microsoft WebBrowser Control.
  4. On the form, click where you want to place the control.
  5. Move and size the control to the area you want to display.

Tip   If the WebBrowser control can’t display the full width or height of a Web page or document, it automatically displays scroll bars. However, in most cases, you should make the control wide enough to display the full width of a typical Web page so that users of your application don’t have to scroll horizontally.

Note   After you add a WebBrowser control to a form and save the form or display it in Form view, the control retains its original size even when you re-open the form in Design view and resize the control. To resize the control, delete the existing control, add a new control, and then resize the new control and set its properties before saving the form.

Displaying Web Pages or Documents in the WebBrowser Control

To display a Web page or document in the WebBrowser control, use the Navigate method in Visual Basic. The syntax for the Navigate method is:

object.Navigate URL

Object is either the name of the WebBrowser control on your form or an object variable that refers to it, and URL is a string expression that evaluates to a valid URL or path. URL can refer to a Web page or other content on the Internet or an intranet, as well as to an Office document, such as a Microsoft Word document.

If URL refers to an Internet protocol and a location on the Internet, Microsoft Access must establish a connection before is can display the document. If the computer running your application is connected to a proxy server (a secure connection to the Internet through a LAN), or if it has a direct connection to the Internet, the WebBrowser control downloads and displays the Web page or other Internet content immediately. If the computer running your application uses a modem and dial-up connection to the Internet, and that connection hasn’t been established beforehand, the WebBrowser control initiates the connection. For example, if the user’s computer uses a modem and The Microsoft Network to connect to the Internet, the Sign In dialog box is displayed to establish the connection to the Internet before the WebBrowser control can display Internet content.

If URL refers to an Internet protocol and a location on an intranet server, the computer running your application must be connected to the intranet and have permission to access that server.

If URL refers to a standard file system path on a local hard drive or intranet, the WebBrowser control opens the document and displays it immediately. The WebBrowser control can open Microsoft Office documents, text files, and HTML documents that don’t require features supported only by an Internet server. For example, the WebBrowser control can’t open HTML documents that use IDC/HTX files or Active Server Pages (ASP) files from the standard file system, but it can open HTML documents that contain only the HTML tags supported by Microsoft Internet Explorer version 3.0.

Note   If URL refers to a path in the standard file system that doesn’t refer to a file name (for example, C:\Windows\System\), the WebBrowser control displays the file system itself, much like My Computer.

Displaying a Document in the WebBrowser Control by Using an Address in a Text Box

Using the WebBrowser control, you can create a form that performs most of the functions of Microsoft Internet Explorer version 3.0. For example, the following illustration shows the Custom Browse form (WebBrowseWeb) in the Developer Solutions sample application.

When a user types a valid URL in the text box at the top of the form (txtLinks) and presses ENTER, the WebBrowser control (ActiveXCtl1) displays the Web page or document. Pressing ENTER triggers the AfterUpdate event of the txtLinks text box; the AfterUpdate event contains the following code which navigates to the URL entered by the user:

Private Sub txtLinks_AfterUpdate()
On Error Resume Next
	If Len(Me!txtLinks) > 0 Then
		Me!ActiveXCtl1.Navigate Me!txtLinks
	End If
End Sub

Error handling is passed to the control itself because it displays the same error messages displayed by Microsoft Internet Explorer version 3.0.

If you prefer to start navigation by clicking a command button instead pressing ENTER, you can use similar code in the button’s Click event.

The Home, Back, Forward, Refresh, and Search buttons on the Custom Browse form use the corresponding GoHome, GoBack, GoForward, Refresh, and GoSearch methods of the WebBrowser control.

See Also   For information on how to view brief descriptions about the properties, methods, and events of the WebBrowser control, see “Viewing Descriptions of the Properties, Methods, and Events of the WebBrowser Control” later in this chapter.

With the Save Location button on the Custom Browse form, you can save the address and a description of the current document to the Links table in the Developer Solutions database. When you click the Save Location button, Microsoft Access checks to see if the URL has been saved previously, and if not, uses the following statement to open the Save Location To Table dialog box:

DoCmd.OpenForm "frmSaveURLDialog", acWindowNormal, , , acFormEdit, acDialog, _	ctlHyper.LocationName & ";" & ctlHyper.LocationURL

The last argument of this statement (ctlHyper.LocationName & ";" & ctlHyper.LocationURL) sets the OpenArgs property to a concatenated string that contains the two values returned by the LocationName and LocationURL properties of the document currently displayed in the Custom Browse form. When the Save Location To Table dialog box opens, code in its Load event parses the OpenArgs property value back into two parts and displays them as the default description and address. When the user clicks OK, the description and address in the Save Location To Table dialog box form are saved in the Hyperlink and Description fields in the Links table.

See Also   For more information on the Custom Browse form, open the Developer Solutions sample application located in the Samples subfolder of your Office folder.

Displaying a Document in the WebBrowser Control by Using a Hyperlink Stored in a Table

Using the WebBrowser control, you can create a form that displays documents specified in hyperlinks stored in a table. For example, the following illustration shows the Browse Saved Hyperlinks (WebBrowseTable) form in the Developer Solutions sample application. You can use the Browse Saved Hyperlinks form to browse addresses saved in the Links table.

When a user clicks a record navigation button at the bottom of the form to move to a new record, the following code in the form’s Current event displays the Web page or document whose address is stored in the current record.

Private Sub Form_Current()
	Dim varFull As Variant, varDescription As Variant
	Dim HyperlinkAddress As String, HyperlinkSubAddress As String
	Dim msg1 As String, msg2 As String, rst As Recordset, strDisplay As String
	
	On Error Resume Next

	Set rst = Me.RecordsetClone
	rst.Bookmark = Me.Bookmark
	varFull = rst!HyperLink

	If IsNull(varFull) Then GoTo Current_Err
	varDescription = rst!Description
	Me!ActiveXCtl1.Navigate HyperlinkPart(varFull, acAddress)

	If Err = 438 Then Exit Sub

	gvarBookMark = Me.Bookmark

Current_Bye:
	Exit Sub
Current_Err:

msg1 = "Invalid hyperlink address. Remove the record described as '"
msg2 = "' from the Links table or edit the hyperlink to supply a valid address."

MsgBox msg1 & rst!Description & msg2
	
		Me.Bookmark = gvarBookMark
		Exit Sub
End Sub

This procedure uses the Navigate method of the WebBrowser control to display the next hyperlink address. However, don’t pass the contents of a Hyperlink field directly to the Navigate method. If a user enters or edits data stored in a Hyperlink field from a datasheet or form, it may contain up to three parts of information separated by the number sign (#). Even if the user doesn’t enter all three parts in the datasheet or form, Microsoft Access automatically stores number signs in the field. If there are number signs in the Hyperlink field, passing the data from the field directly to the Navigate method generates an error. To handle this, the stored value is passed to the HyperlinkPart function to extract the address portion of the saved hyperlink, which is then passed to the Navigate method. If navigation is successful, the form’s Bookmark property value is stored in a public variable. This public variable is used to return to the last record if subsequent navigation fails.

Using code to save data in a Hyperlink field doesn’t automatically save number signs in the field. To preserve the proper functioning of a Hyperlink field in other contexts, you may want to write your code to save number signs before and after a hyperlink address. For an example of how to do this, see the event procedure set for the Click event of the Save Location button (cmdSaveLocation) on the Custom Browse form.

Note   You don’t have to store addresses in a Hyperlink field if you don’t need users to be able to navigate to addresses by clicking them in datasheets or forms, or if you don’t need to save addresses as HTML anchor tags when saving as HTML. As long as an address doesn’t exceed 255 characters, you can store it in a Text field. If an address exceeds 255 characters, you can store it in a Memo field. In either case, you can pass the value stored in the field directly to the Navigate method.

See Also   For more information on the Browse Saved Hyperlinks form, open the Developer Solutions sample application located in the Samples subfolder of your Office folder. For more information on the format of data stored in a Hyperlink field, see “The Hyperlink Field Storage Format” earlier in this chapter.

Viewing Descriptions of the Properties, Methods, and Events of the WebBrowser Control

Like built-in Microsoft Access objects, the WebBrowser control has properties that your application can set or read to determine the control’s characteristics, methods that your application can use to perform operations on the control, and events your application can respond to. You can view brief descriptions of the properties, methods, and events of the WebBrowser control by using the Object Browser.

Important In order for these properties, methods, and events to appear in the Object Browser, you must set a reference to the Microsoft Internet Controls object library. To set this reference, open a module, click References on the Tools menu, and select the Microsoft Internet Controls check box in the Available References box.

Û To view descriptions of the WebBrowser control’s methods, properties, and events

  1. Open a module.
  2. On the View menu, click Object Browser.
  3. In the Project/Library box, click SHDocVw.
  4. In the Classes box, click WebBrowser.

    The Members Of box lists the methods, properties, and events associated with the WebBrowser control.

See Also   For more information on the methods, properties, and events of the WebBrowser control, see http://www.microsoft.com/intdev/sdk/docs/iexplore/. If you purchased Microsoft Access 97 on CD-ROM, you can view a Help file that contains this information by opening the \ValuPack\Access\Webhelp folder on the Setup CD-ROM, and then copying the Iexplore.hlp and Iexplore.cnt files to your hard disk.

Distributing the WebBrowser Control with Your Application

Unlike most other ActiveX controls, you can’t install the WebBrowser control by itself. For an application that uses the WebBrowser control to work, Microsoft Internet Explorer version 3.0 must also be installed on the computer. Microsoft Internet Explorer version 3.0 can be distributed freely, and doesn’t require the payment of royalties or other licensing fees.

See Also   For information on installing Microsoft Internet Explorer version 3.0, see “Adding the WebBrowser Control to a Form” earlier in this chapter.