Microsoft KB Archive/174921

= How To Communicate with a UserDocument from a WebBrowser Control =

Article ID: 174921

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition

-



This article was previously published under Q174921



SUMMARY
This article demonstrates a technique you can use to communicate with your UserDocument running in a WebBrowser control. You can use this technique to pass parameters to your UserDocuments if you are writing an application that has a WebBrowser control in which you display one or more UserDocuments.



MORE INFORMATION
You can manipulate the UserDocument by adding Public Properties and Methods to the UserDocument. You can access these properties and methods from your WebBrowser application by using the Document property of the WebBrowser control. The Document property of the WebBrowser control exposes the Automation object of the currently displayed document.

This article does not give details on creating WebBrowser control or UserDocument applications. Please see the REFERENCES section below for more information.

For the purposes of understanding how this technique can be used, suppose you have following two projects:

PROJECT 1. MYDOC:


 * This is your UserDocument application. The purpose of the UserDocument is to display and edit customer information for a given CustomerID.

PROJECT 2. MYBROWSER:


 * This application contains a form with a WebBrowser control and ListBox control(lstContact). Your UserDocument is launched inside the WebBrowser(brwWebBrowser). The ListBox control displays all the CustomerIDs. When the user clicks on a CustomerID, you want the UserDocument running in the WebBrowser control to pull up that customer.

Step 1: Add a Public Let Property to your UserDocument. You can use a method also. In the example above, open the MYDOC project and add the following to UserDocument code:

Public Property Let CustomerID(ByVal iCustID As String) 'Load the customer data associated with iCustID End Property

Step 2: Invoke the property of the currently-running UserDocument using the Document property of the WebBrowser control. From the example above, modify the MYBROWSER project to handle the Click event of the ListBox:

Private Sub lstContact_Click Dim CustID As String

'retrieve customer id from the listbox CustID = lstContact.List(lstContact.ListIndex)

'tell the userdocument to load the customer brwWebBrowser.Document.CustomerID = CustID End Sub

NOTE: You can also use this technique when you display multiple UserDocuments within the WebBrowser control. Be aware that the Document property may not be available immediately after you use the Navigate method of the WebBrowser control. If this is the case, then you can use the Busy property of the WebBrowser control to wait until the browser has finished loading the document. Also, you can use the NavigateComplete event of the WebBrowser control to signal that the document has finished loading.

